flake8 - ignore warnings for a function

notorious.no picture notorious.no · Jul 1, 2018 · Viewed 11.7k times · Source

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?

Answer

Eugene Yarmash picture Eugene Yarmash · May 19, 2019

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.