简体   繁体   English

flake8 仅对代码块禁用 linter

[英]flake8 disable linter only for a block of code

I have a file in python like:我在 python 中有一个文件,如:

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.当我运行flake8 + hacking我收到一个错误,因为这些行太大了。

If I put this command at the beginning of the file # flake8: noqa all file will be ignored from linter.如果我将此命令放在文件的开头# flake8: noqa所有文件都将被# flake8: noqa忽略。 But I only want to exclude from linter the block where a is declared.但我只想从 linter 中排除声明a的块。

I want to lint the rest of the file, and I cannot put at the end of each fieldx an # noqa: E501 .我想对文件的其余部分进行 lint,但不能在每个fieldx的末尾fieldx # noqa: E501

Some one know how can I solve this?有人知道我该如何解决这个问题? Thanks谢谢

There isn't a way in flake8 to ignore a block of code flake8 没有办法忽略代码块

Your options are:您的选择是:

  1. ignore each line that produces an error by putting # noqa: E501 on it通过在其上放置# noqa: E501来忽略产生错误的每一行

  2. ignore the entire file (but this turns off all other errors as well) with a # flake8: noqa on a line by itself忽略整个文件(但这也会关闭所有其他错误), # flake8: noqa在一行中使用# flake8: noqa

  3. ignore E501 in the entire file by using per-file-ignores :使用per-file-ignores整个文件中的E501

     [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免责声明:我是当前的 flake8 维护者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM