home ‣ If you program in Python, use pychecker login
If you program in Python, use pychecker – a static code analyzer.
Python is a dynamic language so sometimes trivial typos are only caught when executing code with the typo. It’s especially aggravating when it happens at the end of a long-running process.
Pychecker saves times by finding many such problems by analyzing code, without executing it. And it actually works i.e. finds real problems without too many false positives.
My new best practice is to always run pychecker on a modified piece of code before running it. Saved me many unpleasant surprises.
Pychecker tips
Sadly, some 3rd party packages and even standard python libraries contain code that pychecker flags. Since there’s nothing I can do about it, I silence it with the following bash alias on my mac: alias pychecker='pychecker -b boto,contextlib,zipfile,ftplib'. This line goes into my ~/.bash_profile. As you can probably figure out, -b is a list of modules to ignore in the analysis. Those 4 are modules that caused problems for me in the past.
On Windows I had to modify pychecker.bat to put quotes around the full path to python.exe (since it contained spaces and cmd.exe doesn’t like that). I also copied pychecker.bat to a dir in my %PATH% so that I don’t have to type the full path to execute it.
What pychecker finds
Those are the kinds of bugs that pychecker finds (according to the documentation):
- No global found (e.g., using a module without importing it)
- Passing the wrong number of parameters to functions/methods/constructors
- Passing the wrong number of parameters to builtin functions & methods
- Using format strings that don’t match arguments
- Using class methods and attributes that don’t exist
- Changing signature when overriding a method
- Redefining a function/class/method in the same scope
- Using a variable before setting it
- self is not the first parameter defined for a method
- Unused globals and locals (module or variable)
- Unused function/method arguments (can ignore self)
- No doc strings in modules, classes, functions, and methods