0

I am trying to add my own flake8 plugin that lives in the same repository as the code I want to check, and follow the official documentation to do so. Further I have checked other stackoverflow threads for hints (How to use a local flake8 plugin with a python virtual env?), but without success.

I am stuck with the following error message: flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "local" due to No package metadata was found for plugin.plugin.

My repository structure looks like that:

.flake8
dev-scripts (from here I run flake8 in a venv created by poetry)
flake8-plugin
  plugin
    __init__.py
    plugin.py (containing the class Plugin)

I have tried adding a setup.py file to my flake8-plugin folder, but it didn't change anything. And since I use the local-plugins key in my configuration, I assume that I do not need a setup.py.

.flake8:

...

[flake8:local-plugins]
# Code ('MR') must match the prefix of the error ids generated by the plugin, otherwise nothing will be reported:
extension =
    MC = plugin.plugin:Plugin
paths =
    ./flake8-plugin/

plugin.py contains my Plugin class:

...

class Plugin:
    name = __name__
    version: str = importlib.metadata.version(__name__)  

    def __init__(self, tree: ast.AST):
        self._tree = tree

    def run(
        self,
    ) -> typing.Generator[tuple[int, int, str, type[object]]  , None, None]:
        visitor = Visitor()
        visitor.visit(self._tree)

        for line, col, msg in visitor.errors:
            print("error", flush=True)
            yield line, col, msg, type(self)

Obviously I am missing something. What could it be?


  • flake8 version: 5.0.4
  • python version: CPython 3.10.6 on Linux
user3552
  • 3
  • 2

2 Answers2

1

as of flake8 6.0 the metadata is no longer needed on your plugin class -- you can simply remove both name and version -- it will be picked up automatically from the plugin installation

note that your error message no longer exists in the latest version of flake8


disclaimer: I am the primary flake8 maintainer

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
0

Solution:

When I remove the name and version fields of the Plugin class, it works. They were artifacts from the tutorial code on that I am building my plugin.

Actually it was sufficient to replace the version string by a fixed string, e.g. "1.0.0", but I do not need both fields altogether.

user3552
  • 3
  • 2