mccabe icon indicating copy to clipboard operation
mccabe copied to clipboard

Some way to ignore specific marked branches for the purposes of the complexity measure

Open atrigent opened this issue 4 years ago • 0 comments

I was recently fixing a bunch of flake8 violations in a project. Two such failures were B006 and B008, from PyCQA/flake8-bugbear. The way I like to fix these is to change the default value to None and put an if statement within the function that sets the previous default value if the argument value is None. You can probably see where this is going - each argument that I had to do this for resulted in the mccabe complexity going up by 1, which resulted in hitting our max-complexity setting on a couple functions. These changes did not modify the actual behavior of the functions, and in general these branches do not increase the complexity of the function in any meaningful way. I don't want to modify the structure of the code just to work around a spurious linting error. It is also possible to use the condition expression syntax (e.g. arg = [] if arg is None else arg), which this package does not count as a branch, but I find the if statement syntax more readable and prefer it.

Right now the only way to silence this error is to tell flake8 to ignore violations from this package (which flake8 designates as C901) on the offending functions. However, it will continue to be suppressed if the function does happen to grow too complex in the future. I don't want that either. I'm wondering whether it might be possible to add a feature to this package that enables ignoring specific marked branches for the purposes of the complexity measure. This would probably be done with a comment, similar to how flake8's control comments work.

You might argue that the addition of this feature will encourage people to just ignore branches that are inconvenient to them. But this is already the case - many developers will simply set the C901 violation to ignored the moment they see it instead of taking it as a sign that the function has grown too complex and should be simplified. They may even do it on the file or project level. And I've done the same thing - in the case of the work I've been doing recently, these errors are mostly just annoying and do not indicate actual problems, and my only choice was to add flake8 ignores. Adding a way to specify finer-grained ignores will allow those developers, such as myself, who care in principle about measuring complexity to continue making use of this tool.

atrigent avatar Jul 07 '21 00:07 atrigent