Python linter: settings for Flake8

Python logo

When I work on a project or just a script, I try to stay compliant with PEP8.

What’s your linter? Are you just trying to do a code that works but without best practices?

Now, you can use my settings for Flake8

McCabe complexity

I hate very complex code so I use mccabe.

My setting about max-complexity is 1 so I have all complexity.
I like it because I can see where I have an over-complex code… Why accepting 5 when we can do 2 or 3? I refactorize to reduce the complexity.

PEP 8 naming conventions

Yes, the PEP8 has naming conventions so I use pep8-naming to check it via flake8

Flake8 has a part of pytest

Yes, testing flake8 during pytest can be very good and in that case, I use pytest-flake8

No more blind, catch-all except statements

Hardening code is also avoiding blind, catch-all except statements so I use flake8-blind-except

Ordering import statement

It can be crazy but it’s good to respect the standardized way… Welcome to flake8-import-order

No more shadowing Python builtin

It’s a classic like using “list” as an argument but you’re shadowing the built-in “list”.
Better is avoiding any mistake or misunderstanding with flake8-builtins

Flake8 output with colors

It’s better to have color to find the value that we want to see more easily using flake8-colors

More list comprehension

I love Haskell too so list comprehensions are the best thing for me but also using it when it’s the best way. I use flake8-comprehensions

Docstring is important, never miss it anymore

How many times I forget the endpoint at the end of the line. Now, I’ve flake8-docstrings to stay good with my docstrings

Long story about mutable default arguments

I needed a way to check it and clean my code and it’s flake8-mutable
To read more about Mutable Default Arguments

Flake8 for Jupyter Notebooks

Sometimes I use Jupyter Notebooks so I need flake8-nb

Use the best quoting style

Avoiding escaping quotes as per PEP 8 is the best practice to respect and you check it easily with flake8-quotes

Private member access

Never try to access private member flake8-self

String formatting preference

I can tell Flake8 to alert me when I don’t use Flake8 or when I review a code that a dev doesn’t respect the guideline for the project.
It’s flake8-sfs and my setting is f-strings only.

Flake8 config file

My config: ~/.config/flake8

max-line-length = 120
extend-ignore =
    # Ignore f-strings, we like them:
max-complexity = 1
doctests = True
format = ${cyan}%(path)s${reset}:${yellow_bold}%(row)d${reset}:${green_bold}%(col)d${reset}: ${red_bold}%(code)s${reset} %(text)s

Install tools

I use conda but it’s the same with pip… Just replace conda with pip

conda install mccabe pep8-naming pytest-flake8 flake8-blind-except flake8-import-order flake8-builtins flake8-colors flake8-comprehensions flake8-docstrings flake8-mutable flake8-nb flake8-quotes flake8-self flake8-sfs