Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flake8 - ignore warnings for a function

Tags:

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?

like image 412
notorious.no Avatar asked Jul 01 '18 21:07

notorious.no


People also ask

How do I ignore warnings in flake8?

There are two ways to ignore the file: By explicitly adding it to our list of excluded paths (see: flake8 --exclude ) By adding # flake8: noqa to the file.

How do I ignore E501 in flake8?

[flake8] per-file-ignores = # line too long path/to/file.py: E501, This may be easier than using # noqa comments.

Is too complex 11 c901?

Functions that are deemed too complex are functions that have too much branching logic. Branching logic includes if / elif / else and for / while loops.


2 Answers

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.

like image 195
Eugene Yarmash Avatar answered Sep 22 '22 08:09

Eugene Yarmash


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.

like image 29
bogdan.mustiata Avatar answered Sep 21 '22 08:09

bogdan.mustiata