29

I'm trying to ignore warning C901 too complex for only a single function. I've tried just about ever permutation of # noqa: C901 I can see and still the error appears. I wouldq think the # noqa comment above the function (method?) be enough. I even tried placing the comment on the same line as the def declaration like so:

class Klass():

    def my_complex_method(self):  # noqa: C901
        """
        lots of if's and return's
        """

Here is an example of the message I'm getting from flake8:

src/test/_resource.py:147:5: C901 'Resource.render' is too complex (22)
    def render(self, request):  # noqa: C901
    ^

A quick search only yields how to ignore globally or for the entire file. This is not I want because the other functions in the file I do want to catch if it's too complex. Does anyone know how I can resolve my issue?

notorious.no
  • 4,573
  • 3
  • 19
  • 33
  • You can ignore the error for a specific line, but you have to put the `# noqa` comment on the line that throws the error, which probably isn't the `def` line. – kindall Jul 01 '18 at 21:37
  • @kindall its on the `def` line as far as I can tell. I've updated the question with an example of what I'm getting from flake8. – notorious.no Jul 02 '18 at 01:02
  • 1
    Does your function have a decorator? I disabled this warning for such a function by placing the `# noqa` comment on the line of the decorator instead of the line containing `def`. – Max Smolens May 01 '19 at 19:24
  • Whoever turned such a warning, into an error, has little imagination about software engineering. – j riv Apr 30 '22 at 10:43

4 Answers4

22

From the documentation on mccabe (which is used by flake8 under the hood):

To silence violations reported by mccabe, place your # noqa: C901 on the function definition line, where the error is reported for (possibly a decorator).

So you should put the # noqa comment on the line containing def or the line with a decorator.

Eugene Yarmash
  • 131,677
  • 37
  • 301
  • 358
  • Note: `# flake8: noqa: C901` won't work in case the violation is reported on a function decorator line, so make sure to use just `# noqa: C901` instead. – user2340939 Mar 23 '20 at 11:11
14

When searching this for a different error, what worked for me was to put it prefixed by flake8.

So I guess this:

# flake8: noqa: C901
def somefn(...): ...

should work.

bogdan.mustiata
  • 1,501
  • 14
  • 23
5

I can be better to ignore a known and accepted complexity such that any future regressions are caught and can be discussed. The recipe for accepting a McCabe complexity of up to 12:

def my_complex_function () # noqa: max-complexity: 13
    pass
benjaoming
  • 2,039
  • 1
  • 21
  • 28
  • https://stackoverflow.com/a/68168286/5033247 – Smart Manoj Mar 31 '22 at 07:36
  • This is the best answer IMHO, disabling the check entirely is not the best solution, increasing the complexity only for one particular function which cannot be reduced in complexity without sacrificing readability is a lot better! – nemesisdesign May 15 '22 at 20:41
1

Note that if your method is not all on one line, the # noqa would go on the first line of the method, like so:

def my_method(  # noqa: C901
    self,
    variable_name: str = None,
    variable_int: int = None,
    variable_list: list = None,
):
Aron
  • 11
  • 1