Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 915
Kurt Peek Avatar asked Jun 19 '18 00:06

Kurt Peek


People also ask

How do I ignore warnings in flake8?

There are two ways to ignore the file: By explicitly adding it to our list of excluded paths (see: flake8 --exclude ) By adding # flake8: noqa to the file.

How do I ignore E501 in flake8?

[flake8] per-file-ignores = # line too long path/to/file.py: E501, This may be easier than using # noqa comments.

What is flake8 error?

The convention of Flake8 is to assign a code to each error or warning, like the pep8 tool. These codes are used to configure the list of errors which are selected or ignored. Each code consists of an upper case ASCII letter followed by three digits.

What is the flake8 error code ignore extension?

An extension for flake8 that lets you configure (out-of-source) individual error codes to be ignored per file. This is mostly useful when dealing with legacy code, so that you don't have to ignore any existing error globally, but get the benefits of all checks in new files, while you avoid introducing new kind of errors in existing files.

How do I disable flake8 respecting noqa comments?

If we ever want to disable Flake8 respecting # noqa comments, we can refer to flake8 --disable-noqa. If we instead had more than one error that we wished to ignore, we could list all of the errors with commas separating them: Finally, if we have a particularly bad line of code, we can ignore every error using simply # noqa with nothing after it.

Is it possible to combine flake8--select and--ignore?

It’s also possible as of Flake8 3.0 to combine usage of flake8 --select and flake8 --ignore. This chapter of the User Guide aims to educate about how Flake8 will report errors based on different inputs. By default, Flake8 has a list of error codes that it ignores.

What is--ignore in flake8?

This tells Flake8 to ignore any error codes starting with E1, E23 , or W503 while it is running. The documentation for flake8 --ignore shows examples for how to change the ignore list in the configuration file.


1 Answers

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
like image 153
Eugene Yarmash Avatar answered Oct 09 '22 16:10

Eugene Yarmash