-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get the filename
in the Visitor
?
#70
Comments
I've found out. I need to ask The thing is that, there are others fields that I may need: https://flake8.pycqa.org/en/latest/plugin-development/plugin-parameters.html#indicating-desired-data My workaround works fine, but we probably want to have a native setup, so we don't need to override the EDIT: The solution above doesn't work on the tests, just on the plugin itself. 😢 |
I would be interested in this being supported by default as well. My current workaround is to use patched versions of both the Plugin and Visitor classes: import os
from flake8_plugin_utils import Plugin as _Plugin, Visitor as _Visitor
class Plugin(_Plugin):
def __init__(self, tree, filename, *args, **kwargs):
super(Plugin, self).__init__(tree)
self.filename = filename
def run(self):
for visitor_cls in self.visitors:
visitor = self._create_visitor(visitor_cls, filename=self.filename)
visitor.visit(self._tree)
for error in visitor.errors:
yield self._error(error)
@classmethod
def _create_visitor(cls, visitor_cls, filename=None):
if cls.config is None:
return visitor_cls(filename=filename)
return visitor_cls(config=cls.config, filename=filename)
class Visitor(_Visitor):
def __init__(self, config=None, filename=None) -> None:
super(Visitor, self).__init__(config=config)
if not os.path.isabs(filename):
filename = os.path.realpath(filename)
self.filename = filename |
Description
I'm trying to build a plugin, on which I need to know what is the variable I'm looking at. For example:
How can I be sure that the decorator
app
is thatFastAPI
object? What if I have multiple modules, how can I be sure whichFastAPI
object I'm using for the decorator?I thought about mapping the object with the
filename
, so I can check the imports on the files and be sure what the object actually is.Well, the question basically is: how can I get the
filename
in theVisitor
?FWIW, I'm building this package.
Thank you very much for the package! :)
The text was updated successfully, but these errors were encountered: