Skip to content
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

Open
Kludex opened this issue Jul 24, 2021 · 2 comments
Open

How to get the filename in the Visitor? #70

Kludex opened this issue Jul 24, 2021 · 2 comments
Assignees

Comments

@Kludex
Copy link

Kludex commented Jul 24, 2021

  • Date you used flake8-plugin-utils: 24/07/2021
  • flake8-plugin-utils version used, if any: 1.3.2
  • Python version, if any: 3.8
  • Operating System: Ubuntu 20.04

Description

I'm trying to build a plugin, on which I need to know what is the variable I'm looking at. For example:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def home():
    return "Hello world!"

How can I be sure that the decorator app is that FastAPI object? What if I have multiple modules, how can I be sure which FastAPI 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 the Visitor?

FWIW, I'm building this package.

Thank you very much for the package! :)

@Kludex
Copy link
Author

Kludex commented Jul 24, 2021

I've found out. I need to ask flake8 for that field: https://github.com/Kludex/flake8-fastapi/pull/13/files

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 run method?

EDIT: The solution above doesn't work on the tests, just on the plugin itself. 😢

@afonasev afonasev self-assigned this Jan 13, 2022
@stefan6419846
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants