47

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,919
  • 3
  • 20
  • 34
  • 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
  • 3
    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

32

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
  • 142,882
  • 41
  • 325
  • 378
  • 1
    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
23

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,725
  • 17
  • 26
  • 13
    While this does technically work, it seems to disable all Flake8 errors for the entire file. – Jordan Dimov Nov 18 '19 at 16:50
  • 4
    It will disable all the errors. It should be `def somefn(...): # noqa: C901` – pexea12 Nov 09 '20 at 08:42
  • @pexea12 Works only when the function definition is on a single line. If the arguments are indented using newlines. Unlike pylint, putting inside the function body also doesn't work. – Abhijit Sarkar Jun 26 '23 at 08:10
8

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
  • 81
  • 1
  • 1
  • 1
    Additionally, if you have decorators, it goes on the line of the first decorator – M.Vanderlee Jun 24 '22 at 16:15
  • @M.Vanderlee I've noticed this too. However it seems inconsistent, I can't figure out why sometimes it complains about the decorators as well and sometimes it doesn't. – Gabriel G. Jan 26 '23 at 01:06
8

It 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,135
  • 1
  • 21
  • 29
  • 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
  • Using flake8==5.0.4, this seems to behave the same as a bare `# noqa` and disables all flake8 checks on the line rather than modifying the allowed complexity. Do you have a documentation source you can point me to or does it require a specific version? – kcontr Jul 05 '23 at 19:20
  • @kcontr It seems the answer has a syntax error, the last ":" is now replaced with a "=". I cannot find documentation for this, unfortunately. – benjaoming Jul 09 '23 at 15:10