python / python/mypy

Wrong type inference for class with metaclass that acts as descriptor

Aperta
#10,964 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug priority-2-low topic-descriptors topic-metaclasses
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report
Descriptor protocol does not work on metaclasses - defining __get__ method in a metaclass does not make classes using that metaclass work as descriptors.

To Reproduce

  1. Put this code in a py file:
from typing import Any, TYPE_CHECKING, overload

class _IntDescriptorMeta(type):
    def __get__(self, instance: Any, owner: Any) -> int:
        return 123

class IntDescriptorClass(metaclass=_IntDescriptorMeta):
    ...

class IntDescriptor:
    def __get__(self, instance: Any, owner: Any) -> int:
        return 123

class X:
    number_cls = IntDescriptorClass
    number = IntDescriptor()

print(X.number_cls)
print(X().number_cls)
print(X.number)
print(X().number)

if TYPE_CHECKING:
    reveal_type(X.number_cls)
    reveal_type(X().number_cls)
    reveal_type(X.number)
    reveal_type(X().number)
  1. Run it, you should see:
123
123
123
123
  1. Type check it with mypy and see the incorrect output.

Expected Behavior
I expected mypy to infer type of X.number_cls/X().number_cls correctly:

main.py:24: note: Revealed type is "builtins.int"
main.py:25: note: Revealed type is "builtins.int"
main.py:26: note: Revealed type is "builtins.int"
main.py:27: note: Revealed type is "builtins.int"

Note:
To simplify this example, __get__ returns an int no matter if instance is None or not but I assume a fix would also make it work properly with overloads as it does for instances of classes.

Actual Behavior

Mypy does not infer type of X.number_cls/X().number_cls correctly:

main.py:24: note: Revealed type is "def () -> __main__.IntDescriptorClass"
main.py:25: note: Revealed type is "def () -> __main__.IntDescriptorClass"
main.py:26: note: Revealed type is "builtins.int"
main.py:27: note: Revealed type is "builtins.int"

Your Environment

Additional notes
This works properly on pyright 1.1.161+, see the issue: https://github.com/microsoft/pyright/issues/2164

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 eseguendo la riproduzione Python fornita e controllando con mypy i tipi rivelati, usando se utile l'URL mypy-play collegata. Traccia l'inferenza dei tipi di mypy per i descrittori definiti sulle metaclassi e confrontala con quella dei normali descrittori di istanza. Il lavoro è completato quando tutte e quattro le rivelazioni per il descrittore basato su classe riportano builtins.int e il comportamento esistente dei descrittori rimane corretto.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.