2017-02-23 37 views
1

我想为类和函数名称构建自定义格式器。Flake8无法在自定义格式器上加载插件“N8”

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

以下this教程与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="[email protected]", 
    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'.

回答

1

所以密切。在文档的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 ... 

应该工作得很好。然而,你看到的异常是由于该模块没有名为Example的类。您应该调查packages是否会拿起一个单一的文件模块,或者您需要调整您的插件,这样它看起来像:

flake8_example/ 
    __init__.py 
    ... 

作为可为什么你的setup.py没有适当工作。

相关问题