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?
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.
[flake8] per-file-ignores = # line too long path/to/file.py: E501, This may be easier than using # noqa comments.
Functions that are deemed too complex are functions that have too much branching logic. Branching logic includes if / elif / else and for / while loops.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With