9

After upgrading from django 1.3 to django 1.5 I started to see these DeprecationWarnings during the test run:

path_to_virtualenv/lib/python2.6/site-packages/django/http/request.py:193: DeprecationWarning: HttpRequest.raw_post_data has been deprecated. Use HttpRequest.body instead.

I've searched inside the project for raw_post_data and found nothing. So it was not directly used in the project. Then, I've manually went through INSTALLED_APPS and found that raven module still uses raw_post_data and it was the cause, but..

Is it possible to see the cause of DeprecationWarning during the test run? How to make these warnings more verbose?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Can you show how you make the request? There must be something accessing the `raw_post_data` property even though it shouldn't. – Simeon Visser May 05 '13 at 22:18
  • It's simply `self.client.get(url, params)`. I'm pretty sure it's not relevant, because I do make such requests in many test methods, but only this one causes the warning to appear. So I guess this is because something is imported in `libs` that causes the warning. Thank you, anyway. – alecxe May 05 '13 at 22:26
  • 1
    I see. Are you importing anything in `libs` that is related to Django or requests / views ? In Django 1.5 the `raw_post_data` property is not accessed but something could be analysing the request by iterating over all properties. Perhaps `mock`? Or something in `libs`? – Simeon Visser May 05 '13 at 22:28
  • There is a bunch of imports in `libs`, but nothing related to requests/views, except that there is `from django.conf import settings`. And..here it is: raven is the cause - figured it out manually. Thank you, but I still want to know if I could have seen the cause during the test run somehow. I'll update the question. – alecxe May 05 '13 at 22:36
  • I don't think you can. The `raw_post_data` property is accessed but when the `DeprecationWarning` is raised you won't know who accessed it. So there's no flag that can be enabled to make it more verbose. – Simeon Visser May 05 '13 at 22:51
  • Yes, but, as I understand, first I should somehow (may be with the help of `warnings` module) make django treat warnings as errors, then I should get the traceback during this warning and throw it to the stdout. Thank you for help anyway! – alecxe May 08 '13 at 09:30

2 Answers2

24

You can set Python warning control by command line option -W to raise an exception with a traceback on DeprecationWarning like for errors instead of normal simple warning once. Any specific warning can by filtered by message, category, module, line or by a combination of them.

Examples:

python -W error:"raw_post_data has been deprecated" manage.py test

python -W error::DeprecationWarning manage.py test

python -W error:::django.http.request manage.py test

A fine filtering is useful if you want to fix all warnings of one type together by batch editing in many files of a big project.


Python 2.7 and higher ignores DeprecationWarning usually if they are not reanabled, e.g. by -Wd option or by the environment variable export PYTHONWARNINGS="d". That can be useful on development machines but not on production.

hynekcer
  • 14,942
  • 6
  • 61
  • 99
5

This is taken from a similar question.

You can use the warnings modules to raise an error for DeprecationWarning.

Temporarily add the following snippet to the top of your project's urls.py:

import warnings
warnings.simplefilter('error', DeprecationWarning)

The DeprecationWarning will now raise an error, so if debug=True you'll get the familiar yellow Django error page with the full traceback.

Community
  • 1
  • 1
SunnySydeUp
  • 6,680
  • 4
  • 28
  • 32
  • 1
    Yes, I've seen this, but, unfortunately, it doesn't work - when I add this to `urls.py` or `settings.py` I don't see warnings during the test run at all. I'll try to figure out why and accept the answer if it works. Thank you for participation. – alecxe May 08 '13 at 09:27
  • Found it, the problem was in `raven` itself. It was eating all type of exceptions while accessing `raw_post_data` - that's why I saw warnings and I didn't see exceptions at the same place (see [source](https://github.com/getsentry/raven-python/blob/master/raven/contrib/django/client.py#L71)). So, provided solution works. Thank you! – alecxe May 09 '13 at 10:42
  • @SunnySydeUp, decided to accept your answer, but to give a bounty award to hynekcer for his awesome alternative solution. Hope it seems fair to you too. – alecxe May 09 '13 at 10:45