flake8 - ignore warnings for a function
Asked Answered
G

4

55

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?

Genteel answered 1/7, 2018 at 21:33 Comment(4)
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.Carbonization
@Carbonization 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.Genteel
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.Hindward
Whoever turned such a warning, into an error, has little imagination about software engineering.Emelina
R
35

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.

Reposition answered 19/5, 2019 at 13:20 Comment(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.Fuse
D
26

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.

Dalmatic answered 18/10, 2019 at 11:51 Comment(3)
While this does technically work, it seems to disable all Flake8 errors for the entire file.Childers
It will disable all the errors. It should be def somefn(...): # noqa: C901Ericerica
@Ericerica 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.Iodic
I
13

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,
):
Inefficiency answered 28/6, 2021 at 18:16 Comment(2)
Additionally, if you have decorators, it goes on the line of the first decoratorFivespot
@Fivespot 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.Freewheel
S
10

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
Sharla answered 1/8, 2021 at 22:24 Comment(4)
https://mcmap.net/q/333988/-flake8-ignore-warnings-for-a-functionGian
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!Duke
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?Finesse
@Finesse It seems the answer has a syntax error, the last ":" is now replaced with a "=". I cannot find documentation for this, unfortunately.Sharla

© 2022 - 2024 — McMap. All rights reserved.