python / python/mypy

Plugin hook to customize __repr__ of an Instance

Aperta
#6,501 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

For example, in Django, there's this QuerySet object with a lot of methods, and to properly support those methods we need two (and maybe more in the future) generic parameters.

With get_type_analyze_hook I can convert QuerySet[MyModel] into QuerySet[MyModel, MyModel], so in the code user don't need to think about number of generics. But in error messages, all those generics values are present, though they don't have any semantics, it's just implementation detail.

Or, for Field, I need to have two generic params, one for type of __set__, one for __get__, so they appear in error message as (CharField for example)

django.db.models.fields.CharField[Union[builtins.str, builtins.int, django.db.models.expressions.Combinable], builtins.str]

None of this info makes any sense to users, so I want it to just be django.db.models.fields.CharField.

This is monkeypatching version that works for one of the cases, should be simple enough to add hook for that,

def make_queryset_repr_return_only_one_type():
    from mypy.types import TypeStrVisitor

    old_visit_instance = TypeStrVisitor.visit_instance

    def patched_visit_instance(self, t: Instance) -> str:
        if t.type.has_base(helpers.QUERYSET_CLASS_FULLNAME):
            return old_visit_instance(self, helpers.reparametrize_instance(t, [t.args[0]]))
        else:
            return old_visit_instance(self, t)

    TypeStrVisitor.visit_instance = patched_visit_instance

I can make a PR, if you approve it as a viable feature.

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 da TypeStrVisitor.visit_instance e dal meccanismo get_type_analyze_hook esistente, usando il monkeypatch mostrato come riferimento comportamentale. Determina in che modo un hook di plugin supportato personalizzerebbe il rendering di Instance e considera gli esempi QuerySet e Field come criteri di completamento: gli argomenti generici utilizzati solo dall’implementazione non dovrebbero più comparire negli errori di tipo rivolti all’utente.

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
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.