Flake8: Ignore specific warning for entire file
Asked Answered
T

3

211

The Ignoring Errors docs currently list a way of ignoring a particular error for a particular line:

example = lambda: 'example'  # noqa: E731

... and a way of ignoring all errors for an entire file:

# flake8: noqa

from foo import unused
function_that_doesnt_exist()
x = 1+       2

... and a couple of ways, either through config or through command-line options, of disabling a particular error globally across an entire project.

But what if I want to ignore a particular error across the entirety of a single file - for instance, to disable warnings about unused imports in an __init__.py barrel file that just imports a bunch of classes so that code from other packages can import them from it in turn? The docs don't seem to hint at any syntax for this. Is it possible?

Tyranny answered 8/1, 2018 at 16:2 Comment(3)
For your particular example, the officially recommended method (IIRC) is to give an explicit __all__ declaration that lists the imported stuff.Hypochondria
@MarkAmery, did any of the answers work for you?Duteous
@DanielWalker they both work; neither does exactly what I asked for.Tyranny
J
209

As of Flake8 3.7.0 you can do this using the --per-file-ignores option.

Command line example

flake8 --per-file-ignores="project/__init__.py:F401 setup.py:E121"

Or in your config file

per-file-ignores =
    project/__init__.py:F401
    setup.py:E121
    other_project/*:W9

See the documentation here: http://flake8.pycqa.org/en/latest/user/options.html?highlight=per-file-ignores#cmdoption-flake8-per-file-ignores

It is not possible to place a noqa comment for specific codes at the top of a file like you can for individual lines. # flake8: noqa: F401 may at first appear to work, but it's actually being detected as only # flake8: noqa, which means "ignore all messages in the file".

James answered 31/1, 2019 at 6:15 Comment(1)
To store a per-file-ignores entry in a tox.ini file put it in the [flake8] section.Mishamishaan
H
38

Before version 3.7.0, ignoring specific errors was only implemented per-line but not per-file.

The feature was discussed in issue #324 and the project chose not to implement. An implementation was proposed in this merge request, which nobody has followed up on.

However, some extensions have emerged to address the problem:

  • [discontinued] flake8-per-file-ignores lets you ignore specific warning/errors for specific files via an entry in the config.

  • flake8-putty claims to do the same, but hasn't been updated for a while.

Herd answered 8/1, 2018 at 16:41 Comment(1)
From the flake8-per-file-ignores repo: "This flake8 extension has been discontinued in favour of the per-file-ignores option built into flake8 3.7.0 and above. But be aware that the built-in option uses different syntax." Here's a versioned link to the flake8 website's documentation on that option.Tiddly
H
9

I implemented a flake8 plugin flake8-in-file-ignores to allow adding "ignore" rules in the file itself (as opposed to the built-in config approach), the plugin uses the following syntax

# flake8-in-file-ignores: noqa: E731,E123
Halfcaste answered 27/1, 2023 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.