Hey, check out my Modern Python Projects course. It's an extended version of this workshop!
Apart from the code formatters, there are also linters and static code analyzers.
One of the most popular linters is called
Flake8. Unlike black that formats your file and doesn’t really give you any feedback when you are coding, Flake8 works the other way around. It won’t change your code, but it will provide you with real-time warnings in your code editor.
Flake8 combines three different projects:
One of the reasons for Flake8’s popularity is its massive catalog of plugins that can help you further customize it. Some extensions will modify the default behavior of Flake8, while others will give you additional options that you can use when running flake8.
You can find a list of the most popular ones here: awesome-flake8-extension
Some of the available extensions:
.strip()on a multiline string
If you are using a lot of Flake8 plugins between multiple projects, check out FlakeHell or tox. They both let you install and configure all flake8 plugins by modifying one configuration file (that you can then share between projects).
You can install flake8 and its extensions with pip or pipx:
$ pip install flake8 # Or even better $ pipx install flake8 # Install extensions $ pip install flake8-docstrings flake8-isort # Or $ pipx inject flake8 flake8-docstrings flake8-isort
And then run it on files or directories that you want to check:
$ flake8 my_project my_project/setup.py:40:80: E501 line too long (103 > 79 characters) my_project/tests/test_my_project.py:9:1: F401 'my_project.some_function' imported but unused my_project/my_project/some_file.py:1:7: F821 undefined name 'my_var'
Of course, the best way to use Flake8 is to enable is directly in your code editor. VS Code will offer you this option when you open a Python file, but if it doesn’t, run the
Python: Select Linter command.
Flake8 and black are the most popular tools that can make your life easier. They complement each other nicely. But there are some other static analysis tools that you might be interested in:
Pylint is another static analysis tool that ” looks for programming errors, helps enforcing a coding standard, sniffs for code smells, and offers simple refactoring suggestions”. It has some overlap with Flake8, but you can safely use both at the same time.
Pylint is much more strict than Flake8. It will complain if you have “too few public methods”, “too many local variables” or “classes with no __init__ method”. It’s much easier to write code that will make Flake8 happy than it is to write Pylint-compatible code. Especially if you want to use some more “unusual” code constructs. Sometimes you might know what you are doing, but Pylint will still try to protect “you” from “yourself”.
Try running Pylint on your existing project and see if you like it. Its motto is: “it’s not just a linter that annoys you!” but I personally don’t use it - it annoys me ;)
If you are working with frameworks like Django, make sure you install additional plugins for that framework. For example, pylint-django will suppress warning when you use “class Meta” that only contains attributes, that would normally be reported by pylint.
Bandit is a tool designed to find common security issues in Python code. Running it out of the box on your project will give you plenty of false positives. For example, it will complain about assert statements in your pytest files. But once you spend a moment to tweak the settings (ignore some folders or some of the warnings), it will print some possibly useful information about:
If you are a beginner, bandit can be a good tool that will help “review” your code.
If you are already using flake8, there is a flake8-bandit extension that you can use, so you don’t have to install bandit separately.
If you want to make sure that your documentation is written according to the PEP257, you can install pydocstyle.
Just keep in mind that it will complain about missing documentation of every function or module that you didn’t bother to documents.
This one is also available as Flake8 plugin under the name of flake8-docstrings
Can help you find unused code: unused functions, variables, or modules.
It’s another strict linter.
It might be too much for your project, but maybe you will like it.
It’s like Flake8, but instead of
pyflake, it’s using
pylint (it combines