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

plugin point for ClassDef #18729

Open
asottile-sentry opened this issue Feb 24, 2025 · 0 comments
Open

plugin point for ClassDef #18729

asottile-sentry opened this issue Feb 24, 2025 · 0 comments
Labels
feature topic-plugins The plugin API and ideas for new plugins

Comments

@asottile-sentry
Copy link

asottile-sentry commented Feb 24, 2025

Feature

there are various plugin points for ClassDef (either by base class, metaclass, decorators, etc.)

these are useful when one of those are present but it can be difficult to adjust a class's definition otherwise

I would like a plugin hook similar to the existing ones but called based on the fullname of the class itself independent of whether it has a base class / decorator / metaclass etc. to dynamically adjust the class members

Pitch

admittedly the plugin that I'm working on is currently a bit of a hack -- I'm utilizing a mypy plugin to teach it about particular django plugin added attributes to its classes. currently I'm working around this limitation by using a baseclass hook and then searching around for the actual class I care about -- https://github.com/getsentry/sentry/blob/ea729dd46ecdbe030477b6759b00dca6e424ca63/tools/mypy_helpers/plugin.py#L159-L166

it would be nice if I could do something like:

    def get_class_def_hook(self, fullname: str) -> Callable[[ClassDefContext], None] | None:
        if fullname == 'django.http.HttpRequest':
            return _adjust_http_request_members
       elif fullname == 'rest_framework.request.Request':
            return _adjust_request_members
       else:
            return None

the reason I'm raising this now rather than when I wrote that plugin a while ago is I haven't found an appropriate way to adjust a class when it doesn't have a base class, metaclass, or decorator (such as django.http.HttpResponseBase) and I'm currently hooking into a subclass of that and then adjusting the base class but it makes me nervous that this breaks the cache somehow

getsentry/sentry@fdb6490

@cdce8p cdce8p added the topic-plugins The plugin API and ideas for new plugins label Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-plugins The plugin API and ideas for new plugins
Projects
None yet
Development

No branches or pull requests

2 participants