如何添加自定义 Flake8 规则?

How to add custom Flake8 rules?

有什么办法可以给 flake8 添加自定义规则吗?

这里有一堆规则 https://lintlyci.github.io/Flake8Rules/ 但我在 flake8 的 git 仓库中找不到规则的源代码。

我想写一个自定义规则。

您需要编写自己的 Flake8 插件。

来自 Flake8 的文档:

Flake8 is useful on its own but a lot of Flake8’s popularity is due to its extensibility. Our community has developed plugins that augment Flake8’s behaviour. Most of these plugins are uploaded to PyPI. The developers of these plugins often have some style they wish to enforce.

除了提交功能请求并希望它被接受之外,您不能完全向 Flake8 本身添加自定义规则。但它允许您发布一个插件,该插件可以 附加 安装到 Flake8,然后 Flake8 可以找到它并将其与自己的内置检查一起使用。

他们有很好的开发人员文档,说明如何编写:Writing Plugins for Flake8. If it's a custom rule that you want checked, then it's probably a Check Plugin。它接收与内置检查器相同的代码信息,然后您需要编写自己的代码来检查行。

您还可以查看现有Flake8 plugins的源代码以获取灵感,例如: