{"id":190,"date":"2020-06-10T00:25:00","date_gmt":"2020-06-10T05:25:00","guid":{"rendered":"http:\/\/sycured.127.0.0.1.sslip.io\/?p=190"},"modified":"2024-01-13T19:40:17","modified_gmt":"2024-01-14T00:40:17","slug":"python-linter-settings-flake8","status":"publish","type":"post","link":"http:\/\/10.42.0.68:8080\/blog\/python-linter-settings-flake8","title":{"rendered":"Python linter: settings for Flake8"},"content":{"rendered":"\n

When I work on a project or just a script, I try to stay compliant with PEP8.<\/p>\n\n\n\n

What’s your linter? Are you just trying to do a code that works but without best practices?<\/p>\n\n\n\n

Now, you can use my settings for Flake8<\/a><\/p>\n\n\n\n\n\n\n\n

McCabe complexity<\/h2>\n\n\n\n

I hate very complex code so I use mccabe<\/a>.<\/p>\n\n\n\n

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\u2026 Why accept 5 when we can do 2 or 3? I refactorize it to reduce the complexity.<\/p>\n\n\n\n

PEP 8 naming conventions<\/h2>\n\n\n\n

Yes, the PEP8 has naming conventions so I use pep8-naming<\/a> to check it via flake8<\/code><\/p>\n\n\n\n

Flake8 has a part of pytest<\/h2>\n\n\n\n

Yes, testing flake8<\/code> during pytest can be very good and in that case, I use pytest-flake8<\/a><\/p>\n\n\n\n

No more blind, catch-all except statements<\/h2>\n\n\n\n

Hardening code is also avoiding blind, catch-all except for statements so I use flake8-blind-except<\/a><\/p>\n\n\n\n

Ordering import statement<\/h2>\n\n\n\n

It can be crazy but it’s good to respect the standardized way\u2026 Welcome to flake8-import-order<\/a><\/p>\n\n\n\n

No more shadowing Python builtin<\/h2>\n\n\n\n

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<\/a><\/p>\n\n\n\n

Flake8 output with colors<\/h2>\n\n\n\n

It’s better to have color to find the value that we want to see more easily using flake8-colors<\/a><\/p>\n\n\n\n

More list comprehension<\/h2>\n\n\n\n

I love Haskell too so list comprehensions are the best thing for me but also use it when it’s the best way. I use flake8-comprehensions<\/a><\/p>\n\n\n\n

The docstring is important, never miss it anymore<\/h2>\n\n\n\n

How many times do I forget the endpoint at the end of the line? Now, I’ve flake8-docstrings<\/a> to stay good with my docstrings<\/p>\n\n\n\n

Long story about mutable default arguments<\/h2>\n\n\n\n

I needed a way to check it and clean my code and it’s flake8-mutable<\/a>
To read more about
Mutable Default Arguments<\/a><\/p>\n\n\n\n

Flake8 for Jupyter Notebooks<\/h2>\n\n\n\n

Sometimes I use Jupyter Notebooks so I need flake8-nb<\/a><\/p>\n\n\n\n

Use the best quoting style<\/h2>\n\n\n\n

Avoiding escaping quotes as per PEP 8 is the best practice to respect and you check it easily with flake8-quotes<\/a><\/p>\n\n\n\n

Private member access<\/h2>\n\n\n\n

Never try to access private member flake8-self<\/a><\/p>\n\n\n\n

String formatting preference<\/h2>\n\n\n\n

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 guidelines for the project.
It’s
flake8-sfs<\/a> and my setting is f-strings only.<\/p>\n\n\n\n

Flake8 config file<\/h2>\n\n\n\n

My config: ~\/.config\/flake8<\/code><\/p>\n\n\n\n

[flake8]\nmax-line-length = 120\nextend-ignore =\n    # Ignore f-strings, we like them:\n        SFS301\nmax-complexity = 1\ndoctests = True\nformat = ${cyan}%(path)s${reset}:${yellow_bold}%(row)d${reset}:${green_bold}%(col)d${reset}: ${red_bold}%(code)s${reset} %(text)s<\/code><\/pre>\n\n\n\n

Install tools<\/h2>\n\n\n\n

I use conda<\/code> but it’s the same with pip<\/code>\u2026 Just replace conda<\/code> with pip<\/code><\/p>\n\n\n\n

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<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

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<\/p>\n","protected":false},"author":1,"featured_media":105,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"saved_in_kubio":false,"footnotes":""},"categories":[28],"tags":[29],"_links":{"self":[{"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/posts\/190"}],"collection":[{"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/comments?post=190"}],"version-history":[{"count":1,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/posts\/190\/revisions"}],"predecessor-version":[{"id":191,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/posts\/190\/revisions\/191"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/media\/105"}],"wp:attachment":[{"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/media?parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/categories?post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10.42.0.68:8080\/wp-json\/wp\/v2\/tags?post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}