38

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

Rui Martins
  • 3,337
  • 5
  • 35
  • 40
  • though, especially if it's just a test, do those strings _need_ to be that long? – anthony sottile Oct 19 '20 at 16:45
  • 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 one – Rui Martins Oct 20 '20 at 11:05

1 Answers1

47

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

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • 14
    What is the reason behind the decision to not implement this functionality? – 0xcurb Jun 30 '21 at 09:29
  • 1
    complexity, YAGNI – anthony sottile Jun 30 '21 at 11:47
  • 1
    One could make a feature request on the flake8 github repo under Issues to add it. – adam.hendry Jun 06 '22 at 19:42
  • 3
    please don't, thanks -- I'll close it as a duplicate of the other requests for the same thing – anthony sottile Jun 06 '22 at 22:29
  • 2
    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… – Jonas G. Drange Jul 11 '22 at 11:01
  • @JonasG.Drange nah just put the ignore on the end of the docstring -- it will apply to the entire docstring – anthony sottile Jul 11 '22 at 12:52
  • 3
    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? – KalEl Jul 12 '22 at 21:14
  • @KalEl does it say that in my answer as an option? – anthony sottile Jul 12 '22 at 21:19
  • 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). – KalEl Jul 14 '22 at 00:06
  • "Sorry I don't see it" -- yes because there is not – anthony sottile Jul 14 '22 at 00:15
  • 25
    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? – jamesdlin Jul 25 '22 at 23:20
  • people _think_ they need it but do not know of the three supported ways to already do what they want – anthony sottile Jul 25 '22 at 23:59
  • 22
    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”. – Lucas Werkmeister Jul 27 '22 at 21:02
  • 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? – Gringo Suave Nov 14 '22 at 22:39
  • @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 though – anthony sottile Nov 14 '22 at 23:05
  • 2
    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? – Gringo Suave Nov 16 '22 at 01:54