7

I'm editing a Django settings file that looks similar to the following:

# flake8: noqa
from lucy.settings.base import *
from lucy.settings.staging_production import *

# This ensures that errors from staging are tagged accordingly in Airbrake's console
AIRBRAKE.update(environment='staging')

LOGGING['handlers'].update(console={
    'class': 'logging.StreamHandler'
})

This setting lucy/settings/staging.py, extends two other ones and I'd like to keep the 'star imports', so I'd like to ignore error codes E403 and E405 for this file.

However, the only way I see to do that is to add the #noqa: E403, E405 comment to every line that it applies; by writing # flake8: noqa at the top of the file, it ignores all errors.

As far as I can tell from http://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html, it isn't possible to do this, or have I overlooked something?

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • 1
    Normally you do this by using a configuration file, or by writing a script (or `setup.py` handler, or whatever) that runs different `flake8` commands on different sets of input files. As far as I know, there's no way to do what you're trying to do—although it doesn't seem to be an entirely unreasonable request, so you might want to consider filing a feature request. (But first, is there a reason you're using the 3.1.1 docs instead of the current 3.5 docs? They're probably not likely to accept a feature request from someone who's using a 2-year-old version and not willing to upgrade…) – abarnert Jun 19 '18 at 00:43
  • 1
    Also: "… the only way I see to do that is to add the #noqa: E403, E405 comment to every line that it applies …". That's only two lines right at the top of your file in your example. And, even in a larger example, it's unlikely that you're going to be using dozens and dozens of star imports, or scattering them all around your file. So is this really a problem that needs to be fixed in the first place? – abarnert Jun 19 '18 at 00:46
  • 1
    I have always looked for this optionality and never found it. In Pylint it is as simple as `pylint disable=E501,...`. – Brad Solomon Jun 19 '18 at 01:10
  • Regarding the applicability of the E403 and E405, I believe the former is raised for star imports, and the latter is raised if you for example `.update()` something that hasn't been defined or imported before. So errors were being raised for every line in the example file, not just the first two. – Kurt Peek Jun 20 '18 at 05:48

2 Answers2

8

Starting with Flake8 3.7.0, you can ignore specific warnings for entire files using the --per-file-ignores option.

Command-line usage:

flake8 --per-file-ignores='project/__init__.py:F401,F403 setup.py:E121'

This can also be specified in a config file:

[flake8]
per-file-ignores =
    __init__.py: F401,F403
    setup.py: E121
    other/*: W9
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
0

There is no way of specifying that in the file itself, as far as I'm concerned - but you can ignore these errors when triggering flake:

flake8 --ignore=E403,E405 lucy/settings/staging.py
Javier Arias
  • 2,329
  • 3
  • 15
  • 26