1

I want to build a custom formatter for class and function names.

According to this doc it says that the naming convention falls under the N8** warning code.

After following this tutorial with the help of a sublink this is the resultant code

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)

I setup it up by running pip install --editable .

Then to test it out I ran flake8 --format=example main.py

It throws this error:

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

4

1 回答 1

1

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

Flake8 目前关注三组:

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

如果您的插件是向 Flake8 添加检查的插件,您将使用 flake8.extension。如果您的插件自动修复代码中的错误,您将使用 flake8.listen。最后,如果您的插件执行额外的报告处理(格式化、过滤等),它将使用 flake8.report。

(强调我的。)

这意味着你setup.py应该看起来像这样:

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

如果你setup.py正确安装了你的包然后运行

flake8 --format=example ...

应该工作得很好。但是,您看到的异常是由于模块没有名为 Example 的类。您应该调查是否packages会选择单个文件模块,或者是否需要重组插件,使其看起来像:

flake8_example/
    __init__.py
    ...

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

于 2017-02-23T19:14:37.990 回答