Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing Deprecation Warnings Only for a Specific Version When Testing Django

I am about to upgrade from Django 1.9 to 1.10 and would like to test if I have some deprecated functionality.

However using

python -Wall manage.py test

will show tons and tons of warnings for Django 2.0. Is there a way to suppress warnings only for 2.0 or show only warnings for 1.10?

like image 629
vkopio Avatar asked Jan 11 '17 16:01

vkopio


People also ask

How do I ignore deprecation warning in Python?

Use warnings. filterwarnings() to ignore deprecation warnings filterwarnings(action, category=DeprecationWarning) with action as "ignore" and category set to DeprecationWarning to ignore any deprecation warnings that may rise. Leave category unset to ignore all warnings.

How do you add a deprecation warning?

To warn about deprecation, you need to set Python's builtin DeprecationWarning as category. To let the warning refer to the caller, so you know exactly where you use deprecated code, you have to set stacklevel=2 .

What are deprecated warnings?

Deprecation warnings are a common thing in our industry. They are warnings that notify us that a specific feature (e.g. a method) will be removed soon (usually in the next minor or major version) and should be replaced with something else.


2 Answers

Add this to your manage.py:

import warnings
from django.utils.deprecation import RemovedInDjango110Warning

warnings.filterwarnings('always', category=RemovedInDjango110Warning)

Change 'always' to 'default' to ignore redundant messages or to 'error' to cause the program to fail on warnings.

like image 191
Udi Avatar answered Oct 04 '22 22:10

Udi


A good way to limit the output to only show deprecation warnings is running:

python -Wd manage.py check

or

python -Wd manage.py test
like image 24
Christoffer Avatar answered Oct 04 '22 21:10

Christoffer