{"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 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. Yes, the PEP8 has naming conventions so I use pep8-naming<\/a> to check it via Yes, testing Hardening code is also avoiding blind, catch-all except for statements so I use flake8-blind-except<\/a><\/p>\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 It’s a classic like using “list” as an argument but you’re shadowing the built-in “list”. 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 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 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 I needed a way to check it and clean my code and it’s flake8-mutable<\/a> Sometimes I use Jupyter Notebooks so I need flake8-nb<\/a><\/p>\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 Never try to access private member flake8-self<\/a><\/p>\n\n\n\nMcCabe complexity<\/h2>\n\n\n\n
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\nPEP 8 naming conventions<\/h2>\n\n\n\n
flake8<\/code><\/p>\n\n\n\n
Flake8 has a part of pytest<\/h2>\n\n\n\n
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
Ordering import statement<\/h2>\n\n\n\n
No more shadowing Python builtin<\/h2>\n\n\n\n
Better is avoiding any mistake or misunderstanding with flake8-builtins<\/a><\/p>\n\n\n\nFlake8 output with colors<\/h2>\n\n\n\n
More list comprehension<\/h2>\n\n\n\n
The docstring is important, never miss it anymore<\/h2>\n\n\n\n
Long story about mutable default arguments<\/h2>\n\n\n\n
To read more about Mutable Default Arguments<\/a><\/p>\n\n\n\nFlake8 for Jupyter Notebooks<\/h2>\n\n\n\n
Use the best quoting style<\/h2>\n\n\n\n
Private member access<\/h2>\n\n\n\n
String formatting preference<\/h2>\n\n\n\n