Rafael Almeida
Rafael Almeida

Reputation: 5240

Flake8 failed to load plugin "N8" on custom Formatter

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="[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)

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'.

Upvotes: 1

Views: 617

Answers (1)

Ian Stapleton Cordasco
Ian Stapleton Cordasco

Reputation: 28757

So if you're trying to write a formatter you need to read the documentation a bit more closely. In the registering section of the documentation it says:

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.

(Emphasis mine.)

That means your setup.py should look like this:

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

If your setup.py properly installs your package then running

flake8 --format=example ...

Should work just fine. The exception you're seeing, however, is due to the module either not having a class named Example. You should investigate whether packages will pick up a single file module or if you need to restructure your plugin so that it looks like:

flake8_example/
    __init__.py
    ...

As that may be why your setup.py is not working appropriately.

Upvotes: 1

Related Questions