2017-02-23 78 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沒有適當工作。

相關問題