diff options
| author | Fabian Neundorf <CommodoreFabianus@gmx.de> | 2016-07-24 14:27:05 +0000 |
|---|---|---|
| committer | Fabian Neundorf <CommodoreFabianus@gmx.de> | 2016-07-24 16:27:05 +0200 |
| commit | 373eb15573bb320597c5c21a2dc0f2e47266bdd1 (patch) | |
| tree | 78bae515f47a944a383796e5c1d6018be8d4ada5 /src | |
| parent | 698079f87a017dc7170d9c5f14627d71a47b5aba (diff) | |
| download | flake8-373eb15573bb320597c5c21a2dc0f2e47266bdd1.tar.gz | |
Support functions as file plugins too
It is possible to write plugins which are only a function. At the moment they
are called on each line manually. This allows the function also to be called
on each file once. It works similar to creating the class and calling `run` on
it immediately. The plugin function needs to return a generator.
This is based on the original comment in the `FileChecker.run_ast_checks`
method, but slightly modified as the original comment would've called the
return of the function. But the function could return the reports directly.
Diffstat (limited to 'src')
| -rw-r--r-- | src/flake8/checker.py | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/flake8/checker.py b/src/flake8/checker.py index ba412e3..99bacb5 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -466,11 +466,13 @@ class FileChecker(object): for plugin in self.checks.ast_plugins: checker = self.run_check(plugin, tree=ast) - # NOTE(sigmavirus24): If we want to allow for AST plugins that are - # not classes exclusively, we can do the following: - # retrieve_results = getattr(checker, 'run', lambda: checker) - # Otherwise, we just call run on the checker - for (line_number, offset, text, check) in checker.run(): + # If the plugin uses a class, call the run method of it, otherwise + # the call should return something iterable itself + try: + runner = checker.run() + except AttributeError: + runner = checker + for (line_number, offset, text, check) in runner: self.report( error_code=None, line_number=line_number, |
