background preloader

Static Analysis of Python Code

Facebook Twitter

Since Python will happily "compile" code with a variety of "obvious" errors in it,, but not complain until run time when execution actually tries to run the bad statement, it is very important to have tests that drive execution through as much of your code as possible, but there are tools for static analysis of your code too.

These tools will call your attention to problems that you should consider fixing before you run into problems at testing time. Some of the complaints are just annoying. If I want to loop over a set of integers, what's wrong with "i" as the name of the loop variable? So you have to decide which complaints are non-issues and which should be given serious attention. With effort you can tell some of these tools which complaints you don't want to hear again.

Some of these tools will tell you complexity metrics for your code.

You really should pay attention to code with high complexity. How will you ever test it enough?

See also the pearls elsewhere in my collection for test coverage. Snakefood: Python Dependency Graphs. Generate dependency graphs from Python code.

snakefood: Python Dependency Graphs

This dependency tracker package has a few distinguishing characteristics: It uses the AST to parse the Python files. This is very reliable, it always runs.No module is loaded. Loading modules to figure out dependencies is almost always problem, because a lot of codebases run initialization code in the global namespace, which often requires additional setup. Flake8 2.0. The modular source code checker: pep8, pyflakes and co Flake8 is a wrapper around these tools: PyFlakespep8Ned Batchelder's McCabe script Flake8 runs all the tools by launching the single flake8 script.

flake8 2.0

It displays the warnings in a per-file, merged output. Clone Digger. SLOCCount. This is the home page of "SLOCCount", a set of tools for counting physical Source Lines of Code (SLOC) in a large number of languages of a potentially large set of programs. This suite of tools was used in my papers More than a Gigabuck: Estimating GNU/Linux's Size and Estimating Linux's Size to measure the SLOC of entire GNU/Linux distributions, and my essay Linux Kernel 2.6: It's Worth More!

Others have measured Debian GNU/Linux and the Perl CPAN library using this tool suite. SLOCCount runs on GNU/Linux, FreeBSD, Apple Mac OS X, Windows, and hopefully on other systems too. To run on Windows, you have to install Cygwin first to create a Unix-like environment for SLOCCount (Cygwin users: be sure to use ``Unix'' newlines, not ``DOS'' newlines, when you install Cygwin). Static Code Analyzers for Python - Doug Hellmann. Old-school developers remember lint, the static code analysis tool for C programs.

Static Code Analyzers for Python - Doug Hellmann

There are several similar programs available for Python, and they can all help you clean up your act. This month we continue examining Python development tools you have told me you can’t live without. A fair number of you have mentioned that you use a static analysis tool such as PyChecker, pylint, or PyFlakes. I have to admit, I was a bit skeptical of how useful any of them would be with Python. In a past life, when I used to write a lot of C, I used lint occasionally. The Test Program. PyChecker: Python Code Analysis. PyChecker is a cool tool for checking Python scripts for bugs.

PyChecker: Python Code Analysis

It works with Python 2.0 – 2.7. In this article, we will write some crappy code to see what PyChecker can pick up on. Then we’ll improve the code according to what we find until the code checks out. According to PyChecker’s website, it can detect the following problems: No global found (e.g., using a module without importing it)Passing the wrong number of parameters to functions/methods/constructorsPassing the wrong number of parameters to builtin functions & methodsUsing format strings that don’t match argumentsUsing class methods and attributes that don’t existChanging signature when overriding a methodRedefining a function/class/method in the same scopeUsing a variable before setting itself is not the first parameter defined for a methodUnused globals and locals (module or variable)Unused function/method arguments (can ignore self)No doc strings in modules, classes, functions, and methods Getting Started Other Tips.

Pyflakes in Launchpad. Pylint. Pylint is a source code bug and quality checker for the Python programming language.

Pylint

It follows the style recommended by PEP 8, the Python style guide.[2] It is similar to Pychecker but includes the following features: Checking the length of each lineChecking if variable names are well-formed according to the project's coding standardChecking if declared interfaces are truly implemented[3] It is also equipped with the Pyreverse module that allows UML diagrams to be generated from Python code. See also[edit] References[edit] External links[edit] Pep8 0.6. Python style guide checker pep8 is a tool to check your Python code against some of the style conventions in PEP 8.

pep8 0.6

Features Plugin architecture: Adding new checks is easy.Parseable output: Jump to error location in your editor.Small: Just one Python file, requires only stdlib. You can use just the pep8.py file for this purpose.Comes with a comprehensive test suite. Installation You can install, upgrade, uninstall pep8.py with these commands: $ pip install pep8 $ pip install --upgrade pep8 $ pip uninstall pep8 There's also a package for Debian/Ubuntu, but it's not always the latest version. Example usage and output You can also make pep8.py show the source code for each error, and even the relevant text from PEP 8: Pep8nazi 0. Ns-3: check-style.py. The check-style.py script will test and reformat code according to the ns-3 coding style posted at It requires that you install 'uncrustify'.

ns-3: check-style.py

More... The check-style.py script will test and reformat code according to the ns-3 coding style posted at It requires that you install 'uncrustify'.