ignoring functions via special comments
samuelcolvin opened this issue · 9 comments
like # noqa
for PyFlakes and # pragma: no cover
for coverage it would be great if mccabe had a way of ignoring certain functions.
Usage:
- functions are necessarily greater than the complexity level I want to enforce elsewhere.
- Starting to use mccabe to improve existing code bases without having to either have mccabe always fail or have to set a
max-complexity
which is way to high to have a real impact.
(I kind of assumed there would be an existing issue about this but I couldn't see it, sorry if I'm being blind)
sorry if I'm being blind
Unless you're actually physically blind, you're not being blind.
I would prefer to have a configurable list of fully qualified function name to ignore. For example,
ignore_functions = module.submodule.Class.method,module.submodule.function
Humm, that would work too and definitely any solution would be better than no solution.
However, I personally think special comments would be preferable because
- It would be consistent with coverage and pyflakes
- It would be much more obvious to anyone reading the code that "this function is a special case", especially when you might want to include an explanation for why you've broken a rule. Imagine a big project like django decides to use mccabe. Now I come along and submit a PR to improve some function which is fairly performance critical and decide it really has to be one long function - It would be much clearer for future historians if: the code, the explanation of the exception and the exception declaration where all in one place.
It looks like mccabe uses tokenize rather than ast so comments should be fairly easy to parse.
It would be consistent with coverage and pyflakes
Not pyflakes. Pyflakes doesn't respect any special comments. Flake8 does.
good point, sorry.
Just looked again and seen you are using ast
mostly not tokenize.
It's true then that reading comments would require another entire pass of the code with tokenize.
For what it's worth, Flake8 should respect # noqa
on the lines that McCabe reports for complexity and if you're using Flake8 3.0 or newer will even allow you to do # noqa: C901
.
oh wow, that's what I was looking for. Thanks so much.
Maybe worth documenting this more clearly?
Fixed my problem: pydantic/pydantic@daa8e5f
Maybe worth documenting this more clearly?
It's documented very thoroughly by Flake8. I don't know why mccabe would document Flake8's behaviour.
I'm happy to merge a PR that makes this clearer though, without documenting Flake8 here.