python / python/mypy

plugin point for `ClassDef`

Open
#18,729 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-plugins
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

https://github.com/getsentry/sentry/commit/fdb6490dc03bc7f83009b5326c0cd2bcccacbebd

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing mypy's existing ClassDef plugin hooks and ClassDefContext handling; the issue does not name implementation files or tests. Compare this with tools/mypy_helpers/plugin.py around lines 159-166 and the linked commit. Done means the proposed fullname-based hook can adjust classes without a base class, decorator, or metaclass, with cache behavior understood.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.