Flake8 无法在自定义 Formatter 上加载插件 "N8"

Flake8 failed to load plugin "N8" on custom Formatter

我想为 class 和函数名称构建自定义格式化程序。

根据这个 doc 它说命名约定属于 N8** 警告代码。

跟随this tutorial with the help of a sublink之后,这是结果代码

setup.py

from __future__ import with_statement
import setuptools

requires = [
    "flake8 > 3.0.0",
]

setuptools.setup(
    name="flake8_example",
    license="MIT",
    version="0.1.0",
    description="our extension to flake8",
    author="Me",
    author_email="example@example.com",
    url="https://gitlab.com/me/flake8_example",
    packages=[
        "flake8_example",
    ],
    install_requires=requires,
    entry_points={
        'flake8.extension': [
            'N8 = flake8_example:Example',
        ],
    },
    classifiers=[
        "Framework :: Flake8",
        "Environment :: Console",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python",
        "Programming Language :: Python :: 2",
        "Programming Language :: Python :: 3",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "Topic :: Software Development :: Quality Assurance",
    ],
)

flake8_example.py

from flake8.formatting import base

class Example(base.BaseFormatter):
    """Flake8's example formatter."""

    def format(self, error):
        return 'Example formatter: {0!r}'.format(error)

我设置了 运行 pip install --editable .

然后为了测试我运行flake8 --format=example main.py

它抛出这个错误:

flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "N8" due to 'module' object has no attribute 'Example'.

因此,如果您尝试编写 格式化程序,则需要更仔细地阅读文档。在文档的 registering 部分,它说:

Flake8 presently looks at three groups:

  • flake8.extension
  • flake8.listen
  • flake8.report

If your plugin is one that adds checks to Flake8, you will use flake8.extension. If your plugin automatically fixes errors in code, you will use flake8.listen. Finally, if your plugin performs extra report handling (formatting, filtering, etc.) it will use flake8.report.

(强调我的。)

这意味着您的 setup.py 应该如下所示:

entry_points = {
    'flake8.report': [
        'example = flake8_example:Example',
    ],
}

如果您的 setup.py 正确安装了您的软件包,那么 运行

flake8 --format=example ...

应该可以正常工作。但是,您看到的异常是由于模块没有 class 命名的示例。您应该调查 packages 是否会选择单个文件模块,或者您是否需要重组您的插件,使其看起来像:

flake8_example/
    __init__.py
    ...

这可能就是您的 setup.py 无法正常工作的原因。