flake8 disable linter only for a block of code
Asked Answered
A

1

45

I have a file in python like:

def test_constructor_for_legacy_json():
    """Test if constructor works for a legacy JSON in an old database"""

    a = A(**{
        'field1': 'BIG TEXT WITH MORE THAN 500 CHARACTERS....(...)',
        'field2': 'BIG TEXT WITH MORE THAN 500 CHARACTERS....(...)',
        'field3': 'BIG TEXT WITH MORE THAN 500 CHARACTERS....(...)',
        # (...)
        'field1000': 'BIG TEXT WITH MORE THAN 500 CHARACTERS....(...)',
    })

    assert type(a) == A

When I run flake8 + hacking I receive an error because the lines are too big.

If I put this command at the beginning of the file # flake8: noqa all file will be ignored from linter. But I only want to exclude from linter the block where a is declared.

I want to lint the rest of the file, and I cannot put at the end of each fieldx an # noqa: E501.

Some one know how can I solve this? Thanks

Antilepton answered 19/10, 2020 at 13:52 Comment(2)
though, especially if it's just a test, do those strings need to be that long?Quadruplex
Yes, because I want to copy-paste that JSON from an old project, and I don't want to lose time deleting characters or splitting lines. Usually, the JSON will be the same, but I can need to replace to another oneAntilepton
Q
48

There isn't a way in flake8 to ignore a block of code

Your options are:

  1. ignore each line that produces an error by putting # noqa: E501 on it

  2. ignore the entire file (but this turns off all other errors as well) with a # flake8: noqa on a line by itself

  3. ignore E501 in the entire file by using per-file-ignores:

    [flake8]
    per-file-ignores =
         path/to/file.py: E501
    

generally I'd prefer the third one, maybe even sequestering your long-strings into their own file to be ignored


disclaimer: I'm the current flake8 maintainer

Quadruplex answered 19/10, 2020 at 16:44 Comment(17)
What is the reason behind the decision to not implement this functionality?Consociate
complexity, YAGNIQuadruplex
One could make a feature request on the flake8 github repo under Issues to add it.Discontinuous
please don't, thanks -- I'll close it as a duplicate of the other requests for the same thingQuadruplex
I hit this issue when I had to put the signature of a method in the first line of that method's docstring, because the autodoc generated signature failed to build (unknown reference). Sphinx refused to build if I split the signature over multiple lines, or if I tried to ignore 501 for that line. This left me with having to ignore 501 for the entire file. Of course, fixing the broken reference is a better solution, but it was a third party, so non-trivial. So YGNI for extremely convoluted cases…Glacialist
@JonasG.Drange nah just put the ignore on the end of the docstring -- it will apply to the entire docstringQuadruplex
Can we ignore a specific rule, e.g. E501 for the entire file with a comment in the file and not using a separate configuration file?Isidora
@Isidora does it say that in my answer as an option?Quadruplex
Sorry I don't see it - do I misunderstand the answer? I see (2) but it disables flake8 entirely and not just for E501, and (3) and out-of-file config or flake8 invocation change, but no (?) in-the-python-code comment, that only disables E501 for that file (and nothing else).Isidora
"Sorry I don't see it" -- yes because there is notQuadruplex
It's strange to argue YAGNI when it seems evident that a number of people do need it. Regarding complexity, could there a simpler compromise between per-line and per-file ignores, such as separate directives to disable or enable lints for the rest of the file?Conversationalist
people think they need it but do not know of the three supported ways to already do what they wantQuadruplex
None of the three listed ways does the thing people want, and the answer also starts out by admitting that there is no way to do the thing people want: “There isn't a way in flake8 to ignore a block of code”.Furcula
We have a table at the top of the file. Due to that and disabling checks we don't get assistance for the rest of the file. black can do this, why not flake8?Thermo
@GringoSuave black and flake8 are entirely separate projects which have different design choices and features -- it's trivial to sequester your table to a special file thoughQuadruplex
Well, I tried to simplify the situation; but really we have many of these kinds of files. Best to have the table right next to the code that corresponds to it. We use both of these tools, they should complement each other, no?Thermo
Without weighing in on the morality and responsibility of implementing block-noqa in comments, I always end up wanting it for those encysted messy sections that tend to be in corporate unit tests. There are plenty of ways to avoid / work around it and obviously I haven't put any effort into fixing it so ...Heartstrings

© 2022 - 2024 — McMap. All rights reserved.