mypy infers the wrong type for classmethods

Aperta
#3,482 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python
Ambito
devtools

Direzione di ricerca

Start with the simplified classmethod example and compare mypy's inferred type for hi with the classmethod annotation in typeshed's stdlib/3/builtins.pyi. Trace the callable inference path to determine whether the real classmethod type can be preserved, and use a focused regression test for the __func__ access as the completion check.

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

Descrizione

bug topic-descriptors topic-runtime-semantics

We have some code that simplifies to this:

class A:
    @classmethod
    def hi(cls):
        pass

    bye = classmethod(hi.__func__)

(The real code is wrapping an async classmethod with a sync version; there's another async-to-sync wrapper in there.)

Mypy doesn't like this code because it doesn't know that classmethods have a __func__ attribute. The typeshed annotation for classmethod includes __func__, but mypy doesn't infer the real classmethod type for hi (which is the actual type at runtime), it just infers a general callable type.

This may not be easily fixed until we have protocol support and can more easily have various callable subtypes?

I'm also OK if this is closed as "just use cast() if you're accessing private attributes of a classmethod"; it's certainly not high priority.

Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Merge medio
1g 18h
PR unite (30g)
54

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.

Altre issue di python/mypy

Tutte le issue di python/mypy

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.