Skip to content

Support functions as file plugins too

username-removed-214013 requested to merge xZise/flake8:run-functions into master

This is implementing support for functions as suggested in the comment of FileChecker.run_ast_checks. Currently the entry point needs to return something that has a run method (usually a class) but it could easily support also just using functions if the plugin developer don't need to the class overhang.

As mentioned in the commit, this is implementing it slightly differently from the comment (but imho more sensible). The comment would have something like that:

result = plugin(…)
reports = result()

But I remove the last call and assume that the plugin either already returns the reports or uses a generator:

result = plugin(…)
reports = list(result)

Originally a potential plugin would needed to look like this:

def plugin(tree):
    def do_actual_work():
        yield 1, 1, 'T000 Some error', None
    return do_actual_work

The change is now simplifying it into this:

def plugin(tree):
    yield 1, 1, 'T000 Some error', None

Now one curiosity is the fourth element. As far as I can see it is ignored by Flake8 3.x (not sure regarding Flake8 2.x) so I'm not exactly sure what it should be. In the tests I added it is set to type(FUNCTION_OBJECT) so similar to how classes would set that field. But as Flake8 2.x doesn't support functions and Flake8 3.x doesn't seem to care about it I'm wondering if this element should be removed. So to remain backwards compatible it could just remove the fourth element and allow that the generator (or what ever) yields only three elements. So classes could still use four elements for backwards compatibility but functions could just return three. I could probably provide a pretty simple patch to support this.

Merge request reports