Are there static analysis tools for Python?
Question: I am starting to use Python (specifically because of Django) and I would like
to remove the burden for exhaustive testing by performing some static
analysis. What tools/parameters/etc. exist to detect issues at compile time
that would otherwise show up during runtime? (type errors are probably the
most obvious case of this, but undefined variables are another big one that
could be avoided with an in-depth analysis of the AST.)

Obviously testing is important, and I don't imply that tests can be obviated
entirely; however, there are many runtime errors in python that are not
possible in other languages that perform stricter run-time checking -- I'm
hoping that there are tools to bring at least some of these capabilities to
python as well.


Answer: [pylint](http://www.logilab.org/857) is the best such tool I've found. Due to
Python's nature it's difficult to statically analyze it, but it will catch
undefined variables, basic type errors, unused code, etc. You'll want to tweak
the configuration file, as by default it outputs many warnings I consider
useless or harmful.

Here's part of my `.pylintrc` dealing with warning silencing:

    
    
    [MESSAGES CONTROL]
    
    # Brain-dead errors regarding standard language features
    #   W0142 = *args and **kwargs support
    #   W0403 = Relative imports
    
    # Pointless whinging
    #   R0201 = Method could be a function
    #   W0212 = Accessing protected attribute of client class
    #   W0613 = Unused argument
    #   W0232 = Class has no __init__ method
    #   R0903 = Too few public methods
    #   C0301 = Line too long
    #   R0913 = Too many arguments
    #   C0103 = Invalid name
    #   R0914 = Too many local variables
    
    # PyLint's module importation is unreliable
    #   F0401 = Unable to import module
    #   W0402 = Uses of a deprecated module
    
    # Already an error when wildcard imports are used
    #   W0614 = Unused import from wildcard
    
    # Sometimes disabled depending on how bad a module is
    #   C0111 = Missing docstring
    
    # Disable the message(s) with the given id(s).
    disable=W0142,W0403,R0201,W0212,W0613,W0232,R0903,W0614,C0111,C0301,R0913,C0103,F0401,W0402,R0914
    

