python / python/mypy

plugin point for `ClassDef`

Aperta
#18,729 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature topic-plugins
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia tracciando gli hook del plugin ClassDef esistenti di mypy e la gestione di ClassDefContext; l'issue non indica file di implementazione o test. Confronta questo con tools/mypy_helpers/plugin.py intorno alle righe 159-166 e con il commit collegato. Il lavoro è completo quando l'hook proposto basato su fullname può modificare classi senza una classe base, un decoratore o una metaclasse, e il comportamento della cache è compreso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.