notorious.no
notorious.no

Reputation: 5107

flake8 - ignore warnings for a function

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?

Upvotes: 58

Views: 58990

Answers (4)

benjaoming
benjaoming

Reputation: 2215

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

Upvotes: 11

Aron
Aron

Reputation: 141

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,
):

Upvotes: 14

bogdan.mustiata
bogdan.mustiata

Reputation: 1845

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.

Upvotes: 28

Eugene Yarmash
Eugene Yarmash

Reputation: 150111

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.

Upvotes: 36

Related Questions