Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there way to check feature deprecation against django version?

As some features get deprecated with new versions of Django, is there a way to check for that on an existing project code say on github.

Could a tool do that. Is there a way to detect that through testcases.

Would it be possible to do the same against a python version.

I guess one way could be to run through a specific version of django / python using tox and then check for errors.

I am just looking for something more elegant or direct, something like which says - "Note this feature has been deprecated", something which can be done in strongly typed language like Java.

If one wanted to build such a tool, what could be starting point for that, if possible.

like image 829
jethar Avatar asked Apr 22 '14 11:04

jethar


2 Answers

This is how I got tox to run one project of mine against Django 1.6, 1.7 and 1.8 with deprecation warnings on:

[tox]
envlist = {py27,py34}-{django16,django17,django18}

[testenv]
basepython =
           py27: python2.7
           py34: python3.4

deps =
     django16: Django>=1.6,<1.7
     django17: Django>=1.7,<1.8
     django18: Django>=1.8,<1.9

commands =
         python -Wmodule ./manage.py test

The -Wmodule argument causes Python to output each deprecation warning the first time it occurs in a module, which was good enough for me. I was able to deal with instances where I used from django.core.cache import get_cache, which will be gone in Django 1.9.

In cases where -Wmodule outputs too much, you might want to be more selective. Python's documentation gives the lowdown on how to use this argument. I've purposely not used -Werror because this would not just make the individual tests fail but would make my test suite fail to execute any test because the suite uses deprecated features.

like image 85
Louis Avatar answered Sep 30 '22 03:09

Louis


I think this would have to be in the unit tests for your project.

If your tests exercise code that will be deprecated in a future version of Django you will get warnings. If you've jumped to a version of Django where the feature is already deprecated you'll get exceptions and failed tests of course.

You can tell the Python interpreter to promote warnings to exceptions, which would cause tests to fail.

Instructions here to apply the same trick to the popular nosetests test framework.

If you know already (from Django docs) that some code you're writing will need to change depending on Django version it is run under (eg you're distributing a reusable Django app) I would suggest a form of feature detection using try ... except

For example, here I wanted to conditionally use the new transaction.atomic feature from Django >= 1.6: .

As you anticipated, I then run the tests against different versions of Django with the help of Tox.

like image 21
Anentropic Avatar answered Sep 30 '22 05:09

Anentropic