如何告诉flake8忽略评论

sac*_*cuL 4 python pep8 flake8

我在emacs中使用flake8来清理我的python代码。我的评论被标记为错误(E501 line too long (x > 79 characters)),这很烦人。我想知道是否有人知道请flake8忽略单行和多行注释,但是当我的非注释行太长时仍让我知道吗?

提前致谢!

Yaa*_*ler 18

使用内联注释# noqa: E501应该会为您忽略这个问题。

如果您有多行注释,则可以忽略多行字符串末尾的内联,如下所示:

def do_something():
    """
    Long url as a reference.

    References:
        1. https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html#sqs-long-polling
    """  # noqa: E501
    ...
Run Code Online (Sandbox Code Playgroud)

如果您有很长的内嵌注释# noqa: E501,您可以通过使用相同的方式标记注释来忽略:

def do_something():
    """
    Long url as a reference.

    References:
        1. https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html#sqs-long-polling
    """  # noqa: E501
    ...
Run Code Online (Sandbox Code Playgroud)

^ 奇怪的是,你需要添加第二个#才能工作......

  • 你的回复更好,因为它提到了如何支持多行评论。 (2认同)

sac*_*cuL 9

我已经找到了可能的解决方案,但是可能会有更好的解决方案。如果您写的注释会引发E501错误,即它太长,则可以在该行后附加# noqa: E501,并且flake8会忽略它。例如:

# This is a really really long comment that would usually be flagged by flake8 because it is longer than 79 characters

通常会提高E501,但是

# This is a really really long comment that would usually be flagged by flake8 because it is longer than 79 characters # noqa: E501

将不会。

记录在这里

  • 当多行文档字符串中的一行太长时,这似乎不起作用,是否有不同的语法? (2认同)

Eug*_*ash 7

您可以flake8使用配置文件来更改忽略的代码列表。例如,在您的项目目录中创建一个文件.flake8,其内容如下:

[flake8]
ignore =
    E121,E123,E126,E226,E24,E704,W503,W504,  # these are ignored by default
    E501,  # line too long
per-file-ignores =
    path/to/file.py: F841
Run Code Online (Sandbox Code Playgroud)

这可能比使用# noqa注释更容易。

  • 这不会回答OP,因为它还会抑制非注释行上的E501 (5认同)