GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing flake8.pycqa.org — Selecting and Ignoring Violations — flake8 7.5.0 documentation Selecting and Ignoring ViolationsIt is possible to select and ignore certain violations reported by Flake8
and the plugins we’ve installed. It’s also possible as of Flake8 3.0 to
combine usage of Ignoring Violations with Flake8By default, Flake8 has a list of error codes that it ignores. The list used
by a version of Flake8 may be different than the list used by a different
version. To see the default list, Extending the Default Ignore ListIf we want to extend the default list of ignored error codes, we can use
flake8 --extend-ignore=E1,E23 path/to/files/ path/to/more/files
This tells Flake8 to ignore any error codes starting with Overriding the Default Ignore ListIf we want to completely override the default list of ignored error codes, we
can use flake8 --ignore=E1,E23,W503 path/to/files/ path/to/more/files/
This tells Flake8 to only ignore error codes starting with Note The documentation for In-line Ignoring ErrorsIn some cases, we might not want to ignore an error code (or class of error codes) for the entirety of our project. Instead, we might want to ignore the specific error code on a specific line. Let’s take for example a line like example = lambda: 'example' Sometimes we genuinely need something this simple. We could instead define a function like we normally would. Note, in some contexts this distracts from what is actually happening. In those cases, we can also do: example = lambda: 'example' # noqa: E731 This will only ignore the error from pycodestyle that checks for lambda
assignments and generates an Note If we ever want to disable Flake8 respecting If we instead had more than one error that we wished to ignore, we could list all of the errors with commas separating them: # noqa: E731,E123
Finally, if we have a particularly bad line of code, we can ignore every error
using simply Contents before and after the # mypy requires `# type: ignore` to appear first x = 5 # type: ignore # noqa: ABC123 # can use to add useful user information to a noqa comment y = 6 # noqa: ABC456 # TODO: will fix this later Ignoring Entire FilesImagine a situation where we are adding Flake8 to a codebase. Let’s further imagine that with the exception of a few particularly bad files, we can add Flake8 easily and move on with our lives. There are two ways to ignore the file:
The former is the recommended way of ignoring entire files. By using our
exclude list, we can include it in our configuration file and have one central
place to find what files aren’t included in Flake8 checks. The latter has the
benefit that when we run Flake8 with Selecting Violations with Flake8Flake8 has a default list of violation classes that we use. This list is:
This list can be overridden by specifying Let’s look through some examples using this sample code: # example.py def foo(): print( "Hello" "World" ) By default, if we run flake8 example.py
example.py:4:9: E131 continuation line unaligned for hanging indent Now let’s select all flake8 --select E example.py
example.py:3:17: E126 continuation line over-indented for hanging indent example.py:4:9: E131 continuation line unaligned for hanging indent example.py:5:9: E121 continuation line under-indented for hanging indent Suddenly we now have far more errors that are reported to us. Using
We can also be highly specific. For example, we can do flake8 --select E121 example.py
example.py:5:9: E121 continuation line under-indented for hanging indent We can also specify lists of items to select both on the command-line and in our configuration files. flake8 --select E121,E131 example.py
example.py:4:9: E131 continuation line unaligned for hanging indent example.py:5:9: E121 continuation line under-indented for hanging indent Selecting and Ignoring Simultaneously For Fun and ProfitPrior to Flake8 3.0, all handling of pycodestyle has always preferred # example.py import os def foo(): var = 1 print( "Hello" "World" ) If we run Flake8 with its default settings we get: flake8 example.py
example.py:1:1: F401 'os' imported but unused example.py:5:5: F841 local variable 'var' is assigned to but never used example.py:8:9: E131 continuation line unaligned for hanging indent Now let’s select all flake8 --select E,F example.py
example.py:1:1: F401 'os' imported but unused example.py:5:5: F841 local variable 'var' is assigned to but never used example.py:7:17: E126 continuation line over-indented for hanging indent example.py:8:9: E131 continuation line unaligned for hanging indent example.py:9:9: E121 continuation line under-indented for hanging indent Now let’s selectively ignore some of these while selecting the rest: flake8 --select E,F --ignore F401,E121 example.py
example.py:5:5: F841 local variable 'var' is assigned to but never used example.py:7:17: E126 continuation line over-indented for hanging indent example.py:8:9: E131 continuation line unaligned for hanging indent Via this example, we can see that the most specific user-specified rule will win. So in the above, we had very vague select rules and two very specific ignore rules. Let’s look at a different example: flake8 --select F401,E131 --ignore E,F example.py
example.py:1:1: F401 'os' imported but unused example.py:8:9: E131 continuation line unaligned for hanging indent In this case, we see that since our selected violation codes were more specific those were reported. |
| You are visitor 01402605 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |