Is it possible to ignore only certain error codes for entire files in Flake8?
Asked Answered
P

2

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?

Psychotechnics answered 19/6, 2018 at 0:4 Comment(4)
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…)Parthinia
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?Parthinia
I have always looked for this optionality and never found it. In Pylint it is as simple as pylint disable=E501,....Cyrie
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.Psychotechnics
A
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
Acadia answered 17/4, 2019 at 15:7 Comment(0)
M
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
Marquess answered 26/10, 2018 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.