flake8: Ignore only F401 rule in entire file
Asked Answered
P

3

46

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.

Potash answered 4/12, 2019 at 0:36 Comment(0)
C
67

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)

Cutshall answered 21/12, 2019 at 18:28 Comment(17)
I'd say that's less of a disclaimer and more of an authoritative statement :)Lavoie
@Lavoie yep -- unfortunately (or fortunately?) the SO rules require such a disclosure ;)Cutshall
OK. That helps. Thanks! Would you consider a feature request for the per-file specification living at the top of the file itself? (If not for F401 in particular, then maybe for other errors.)Potash
What's the flake8 file? .flake8.cfg?Mcdougal
@Mcdougal setup.cfg, tox.ini, or .flake8 (found by googling "flake8 configuration files")Cutshall
Exporting 3rd party modules as this file public objects on 'import *'. Looks like side effect exploitation. Also might confuse automatic import suggestion in PyCharm and bring cyclic imports.Hauge
Using __all__ may not be that better. You would have a new spot for inconsistency. See this discussion for example.Lacefield
@Lacefield you're incorrect -- pyflakes will flag incorrect things in __all__ if they're mistypedCutshall
@AnthonySottile my point is not that there isn't a tool that can alleviate this. I'm just noting that without __all__, I have only one place to put the name of my symbol, while with __all__ I have two places, and they have to be consistent. So an inconsistency may arise at any time they differ. The discussion I linked to concluded that the use of __all__ was not really a solution precisely because of that.Lacefield
right -- I'm telling you that you're wrong: the inconsistency cannot arise because pyflakes enforces that all members in __all__ are public symbolsCutshall
@AnthonySottile we are probably arguing about semantics here. From my perspective, as soon as names differ in __all__ and import, there is an inconsistency, it doesn't matter how clear pyflakes will eventually make it after the fact. More inconsistencies mean more maintenance work, and this alone can be the reason a project (like the one I linked to) may not consider using __all__ a "better way".Lacefield
I don't think we're arguing semantics, you're inventing problems that don't existCutshall
Well, they do exist for people I mentioned and you keep ignoring. Anyway, my comment was really for people not that biased as the maintainer of the library in question. I guess we both know now why SO demands such a disclosure :)Lacefield
the people in that thread are unaware of the enforced consistency and don't understand the point of __all__ which isn't pyflakes specificCutshall
is there a pyflakes equivalent to the per-file-ignores solution?Longerich
pyflakes has no configuration nor optionsCutshall
I think, using __all__ is just a bandaid. It looks like it promotes from module import * a level upCamail
S
9

According to the Documentation it's as easy as changing # noqa by:

# noqa: F401
Sjoberg answered 4/12, 2019 at 0:41 Comment(4)
I thought that was just for a single line, not the entire file. What's the right syntax for the whole file? For instance, # flake8: noqa: F401 at the top of the file still ignores all rules.Potash
You can run it on the console like this: flake8 --ignore=F401 your_script.pySjoberg
Yeah, I'm aware of that syntax, but I'm looking for a way to denote this in the file, so that I don't have to modify the command line string.Potash
@AJFriend Your assumption is correct, that there is no possible syntax to ignore a single rule within a single file declared inside the file. This answer is incorrect.Cutaneous
H
0

Default config doesn't allow to specify the 'ignores' in the file itself, only in the general config but this plugin allows it (I'm the author)

https://github.com/bagerard/flake8-in-file-ignores

Humane answered 2/6 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.