如何告诉 flake8 忽略评论

How to tell flake8 to ignore comments

我在 emacs 中使用 flake8 来清理我的 python 代码。我发现我的评论被标记为错误很烦人 (E501 line too long (x > 79 characters))。我想知道是否有人知道如何善意地要求 flake8 忽略单行和多行评论,但当我的非评论行太长时仍然让我知道?

提前致谢!

我已经想出了一个可能的解决方案,但可能还有更好的方法。如果你写的注释会引发 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

不会。

已记录 here

您可以使用 configuration file 更改被 flake8 忽略的代码列表。例如,在您的项目目录中创建一个名为 .flake8 的文件,内容如下:

[flake8]
per-file-ignores =
    # line too long
    path/to/file.py: E501,

这可能比使用 # noqa 评论更容易。