Consider the following sample code:
data = []
try:
print data[0]
except IndexError as error:
print error.message
There is nothing syntactically wrong (using Python2.7) with the code except that if you run python with warnings turned on, you would see a DeprecationWarning
:
$ python -W always test.py
test.py:5: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
print error.message
list index out of range
FYI, this is because .message
was deprecated since python2.6 and removed in python3.
Now, I'd like to find all places in the project where .message
is called on any exception instance by using static code analysis tools. As an end goal, I'm planning to have this check running as a part of a daily build&test&code quality check task and raise an error if the syntax is still used.
Is it possible? Is it something that pylint
, pyflakes
or other code analysis tools are capable of?
I found that pep8
tool has several similar checks implemented, for instance, has_key()
usage check:
$ cat test.py
my_dict = {}
print my_dict.has_key('test')
$ pep8 test.py
test.py:2:14: W601 .has_key() is deprecated, use 'in'
As an alternative solution, I can treat all warnings as errors (like suggested here) and make my tests fail but this has its disadvantages:
- there are other deprecation warnings coming from third-party packages that I cannot fix
- strictly speaking, this requires 100% coverage, which is difficult to maintain