40

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.

AJ Friend
  • 703
  • 1
  • 7
  • 16

2 Answers2

56

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)

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • 11
    I'd say that's less of a disclaimer and more of an authoritative statement :) – chepner Dec 21 '19 at 18:52
  • @chepner yep -- unfortunately (or fortunately?) the SO rules require such a disclosure ;) – anthony sottile Dec 22 '19 at 01:39
  • 4
    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.) – AJ Friend Dec 29 '19 at 07:06
  • What's the flake8 file? .flake8.cfg? – theX Aug 05 '20 at 00:39
  • @theX [`setup.cfg`, `tox.ini`, or `.flake8`](https://flake8.pycqa.org/en/latest/user/configuration.html#configuration-locations) (found by googling "flake8 configuration files") – anthony sottile Aug 05 '20 at 00:55
  • 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. – Denis Barmenkov Feb 02 '21 at 10:41
  • Using `__all__` may not be that better. You would have a new spot for inconsistency. See [this discussion](https://github.com/RaRe-Technologies/gensim/issues/1551) for example. – sourcream May 27 '22 at 13:37
  • @sourcream you're incorrect -- pyflakes will flag incorrect things in `__all__` if they're mistyped – anthony sottile May 27 '22 at 14:29
  • @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. – sourcream May 27 '22 at 14:49
  • right -- I'm telling you that you're wrong: the inconsistency cannot arise because `pyflakes` enforces that all members in `__all__` are public symbols – anthony sottile May 27 '22 at 14:57
  • @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". – sourcream May 27 '22 at 15:24
  • I don't think we're arguing semantics, you're inventing problems that don't exist – anthony sottile May 27 '22 at 15:48
  • 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 :) – sourcream May 27 '22 at 16:12
  • the people in that thread are unaware of the enforced consistency and don't understand the point of `__all__` which isn't pyflakes specific – anthony sottile May 28 '22 at 05:23
  • is there a pyflakes equivalent to the per-file-ignores solution? – db_ Dec 06 '22 at 11:38
  • pyflakes has no configuration nor options – anthony sottile Dec 06 '22 at 16:15
4

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

# noqa: F401
Juan C
  • 5,846
  • 2
  • 17
  • 51
  • 6
    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. – AJ Friend Dec 04 '19 at 01:12
  • You can run it on the console like this: `flake8 --ignore=F401 your_script.py` – Juan C Dec 04 '19 at 02:25
  • 10
    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. – AJ Friend Dec 04 '19 at 18:36
  • 1
    @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. – Taylor D. Edmiston Feb 19 '23 at 20:33