py_static_check: Statically check your Python code for errors

I have released py_static_check, an useful tool that can statically check your Python code for common errors. Statically implies that the code isn't evaluated.

py_static_check is based on pyflakes and adds following features:

  • Ability to specify what star imports resolve to (-s argument). pyflakes does not work with star imports
  • Ability to ignore unused import warnings (-i argument). Checking a big code base will pollute your warnings with unsued import warnings
  • Better sorting of warnings/errors

To install it do following:

$ sudo easy_install py_static_check

To fork it (and improve it) go to github:

Here are some of the things py_static_check can do.

Catch undefined names, even for star imports

Example code:

from os import *

def function_with_error():
    print path
    print paths

star_imports.py:

import os
STAR_IMPORTS = {
    'os': os.__all__,
}

Running it with py_static_check (with -s argument):

$ py_static_check -s tests/star_import.py tests/undefined_name_star.py
tests/undefined_name.py:5: undefined name 'paths

Running it with pyflakes:

$ pyflakes tests/undefined_name_star.py 
tests/undefined_name_star.py:6: 'from os import *' used; unable to detect undefined names

Ignore not used warnings

Example code:

from os import path

Running it with py_static_check (with -i argument):

$ py_static_check -i tests/ignore_not_used.py

$ py_static_check tests/ignore_not_used.py
tests/ignore_not_used.py:10: 'path' imported but unused

Assigned but never used

Like pyflakes it can catch a lot of errors, such as defining a variable without using it.

Example code:

def some_function():
    def inner_fn():
        local_var = ""

Running it with py_static_check:

$ py_static_check tests/assigned_but_never_used.py
tests/assigned_but_never_used.py:8: local variable 'local_var' is assigned to but never used

19. Dec 2011 Announcements · Code · Code improvement · Code rewrite · Python · Tips
© Amir Salihefendic