Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flake8: Ignore only F401 rule in entire file

Tags:

python

flake8

Is there a way to get flake8 to ignore only a specific rule for an entire file? Specifically, I'd like to ignore just F401 for an entire file.

I have a file like __init__.py where I import symbols that are never used within that file. I'd rather not add # noqa to each line. I can add # flake8: noqa to the beginning of the file, but that ignores all rules. I'd like to ignore just the F401 rule.

like image 911
AJ Friend Avatar asked Dec 04 '19 00:12

AJ Friend


People also ask

How do you ignore one line on 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.

How do you override flake8?

Selecting Violations with Flake8 This list can be overridden by specifying flake8 --select . Just as specifying flake8 --ignore will change the behaviour of Flake8, so will flake8 --select . Suddenly we now have far more errors that are reported to us. Using --select alone will override the default --ignore list.

What is flake8 NOQA?

# flake8: noqa : files that contain this line are skipped. lines that contain a # noqa comment at the end: will not issue warnings.


1 Answers

there is not currently a way to do what you're asking with only source inside the file itself

the current suggested way is to use the per-file-ignores feature in your flake8 configuration:

[flake8] per-file-ignores =     */__init__.py: F401 

Note that F401 in particular can be solved in a better way, any names that are exposed in __all__ will be ignored by pyflakes:

from foo import bar  # would potentially trigger F401 __all__ = ('bar',)  # not any more! 

(disclaimer: I'm the current maintainer of flake8 and one of the maintainers of pyflakes)

like image 87
Anthony Sottile Avatar answered Oct 06 '22 00:10

Anthony Sottile