Description
flake8 supports checking the McCabe complexity metric but it's historically disabled by default.
https://flake8.pycqa.org/en/2.5.5/#quickstart and https://en.wikipedia.org/wiki/Cyclomatic_complexity suggest that it's recommended to set max-complexity=10
in the config.
I use it in many projects and I like how it helps to keep the code simple. Of course, it's harder to apply this against a legacy codebase but it's worth it. In case, when there's many violations, the strategy I normally use is applying it gradually: you can use per-file-ignores
for temporary module-global exclusions and noqa
markers when it makes sense to be more granular, and then I eliminate each of those over time.
P.S. There's other complexity checks that are provided via third-party plugins but cyclomatic complexity is a good start. See https://sobolevn.me/2019/10/complexity-waterfall.