Verbose deprecation warnings in Django
Asked Answered
G

2

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?

Groningen answered 5/5, 2013 at 22:1 Comment(6)
Can you show how you make the request? There must be something accessing the raw_post_data property even though it shouldn't.Eyewitness
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.Groningen
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?Eyewitness
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.Groningen
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.Eyewitness
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!Groningen
B
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.

Baize answered 8/5, 2013 at 0:19 Comment(3)
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.Groningen
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). So, provided solution works. Thank you!Groningen
@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.Groningen
L
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.

Lacefield answered 9/5, 2013 at 8:14 Comment(0)
B
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.

Baize answered 8/5, 2013 at 0:19 Comment(3)
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.Groningen
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). So, provided solution works. Thank you!Groningen
@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.Groningen

© 2022 - 2024 — McMap. All rights reserved.