Analyzing Python code with Pylint

Claudiu Popa (~claudiu)


22

Votes

Description:

Given the dynamic nature of Python, some bugs tend to creep in our codebases. Innocents NameErrors or hard-to-find bugs with variables used in a closure, but defined in a loop, they all stand no chance in front of Pylint (http://pylint.org/).

In this talk, I’ll present one of the oldest static analysis tools for Python, with emphasis on what it can do to understand your Python code. Pylint is both a style checker, enforcing PEP 8 rules, as well as a code checker in the vein of pyflakes and pychecker, but its true power isn’t always obvious in the eye of beholder, especially when it's hidden through its verbosity. Interpreting its results can be a daunting task, but there are tricks which can be used to improve its user experience, such as enabling only structural checking with the -E flag or disabling unwanted category checks.

Pylint can detect simple bugs such as unused variables and imports, but it can also detect more complicated cases such as invalid arguments passed to functions, it understands the method resolution order of your classes, generators, contexts managers and what special methods aren’t implemented correctly.

Starting from abstract syntax trees, we’ll go through its inference engine and we’ll see how Pylint understands the logical flow of your program and what sort of type hinting techniques are used to improve its inference, including PEP 484 type hints. Pylint's roadmap includes better understanding of Python code, by improving its flow control analysis, escape analysis, understanding metaclasses and descriptors and having a better type checker, as well as improving the user experience, by reducing the number of false positives it currently has. As a bonus, I’ll show how it can be used to help you port your long-forgotten library to Python 3, using its new –py3k mode, which emits warnings regarding Python 3 compatibility.

The talk overview will contain the following aspects:

  • Why doing static analysis for Python, how does it help us as programmers.
  • A little bit about other tools, such as pep8, flake8, pyflakes, pychecker, mypy and how Pylint is different than them and how it cooperates with them.
  • General architecture of Pylint:

      * abstract syntax trees
      * transforms of the trees
      * the inference engine (astroid)
      * pylint's checkers
      * the plugin interface
    
  • The types of bugs that Pylint can detect and how can we interpret the reports generated by it and what to focus on in the output for most effective static fixes on the code.

  • How can it be used to detect Python 3 compatibility problem in your Python 2 application, using pylint --py3k
  • Its future roadmap, including: a symbolic interpreter for "executing" abstract syntax trees, something similar to what RPython does, in order to collect flow control information and runtime manipulators, such as list.append or ABC.register.

Prerequisites:

The participants should have a basic understanding of Python. No other prerequisite is necessary, since the other concepts will be explained during the talk.

The following concepts will be explained during this talk:

  • abstract syntax trees
  • static analysis and why using static analysis for your code
  • what is inference and what forms an inference engine

Speaker Info:

I'm an open source enthusiast coming from Romania. Lead maintainer of the Pylint project and on my way to becoming a CPython core contributor someday, I'm also interested in language design, interpreters, static analysis and how languages work under the hood, as well as in building a community around a technology, being the founder of the biggest romanian Python group, RoPython.

Section: Others
Type: Talks
Target Audience: Intermediate
Last Updated: