Lately I worked on a project that enforces flake8 conformance. And I learned to embrace it and think it is a good idea to use it for my own projects, too.
What is flake8
flake8 is a code analyzer that check if the code conforms to the pep8 rules.
Setup
As usual, there are some rules that I don’t like (e.g. line length). But thankfully, it is possible to configure this. This is done by adding a file .flake8
in the root of your repo, or even in its parent folder, if you have a lot repos.
Mine looks like this:
[flake8]
ignore = W191 E501 E722 W503
And if you use Visual Studio Code (it has extension for flake8 and shows the results inline, I find that very useful, (but if anyone knows an other IDE that can do that, too?).
this is my settings file (.vscode/settings.json
):
{
"editor.insertSpaces": false,
"editor.formatOnSave": false,
"editor.formatOnType": false,
"flake8.args": [
"--ignore=W191,E501,E722,W503",
"--verbose"
],
}
But flake8 can be run from the command line, too.
Why all that
All this is mostly about code formatting, readability and such. But there are a few things that flake8 can find that are real bugs. The most important is F821. But F841 is also useful.
Automatic Code Formatting
I found one tool that is highly configureable and thous the most useful for me: autopep8.
I use this prompt in the terminal:
autopep8 --in-place --select E221,E226,E225,E201,E202,E302,E305,E251,E261,W292,W293 path/to/file.py
or with the -r
argument followed by a path to a folder. That will process all files in that folder.
autopep8 --in-place --select E221,E226,E225,E201,E202,E302,E305,E251,E261,W292,W293 -r path/to/folder
This will only tough issues with the peps in the command. Feel free do add/remove things.