MyPy reports errors when performing nested element access using a ``TypeVar``-parameterized generic type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
Type variables and generics enable propagating finer-grained type information through objects like containers. However, it seems that MyPy has issues when this design pattern is used in a nested manner:
To Reproduce
import typing
T = typing.TypeVar("T")
class Container(typing.Generic[T]):
def __init__(self, value : T) -> None:
self.value = value
class A(Container[int]):
pass
class B(Container[A]):
pass
def get(x: Container[T]) -> T:
return x.value
a = B(A(5))
reveal_type(a)
b = get(a)
reveal_type(b)
c1 = get(b)
reveal_type(c1)
# This is expected to match 'c1'
c2 = get(get(a))
reveal_type(c2)
MyPy-Play link: https://gist.github.com/mypy-play/40d62fe7a0fc28bf68bd9d156ba77c5b
a.py:20: note: Revealed type is "a.B"
a.py:23: note: Revealed type is "a.A"
a.py:26: note: Revealed type is "builtins.int"
a.py:30: note: Revealed type is "builtins.int"
Reference (PyRight):
/Users/wjakob/drjit-nanobind/a.py
/Users/wjakob/drjit-nanobind/a.py:20:13 - information: Type of "a" is "B"
/Users/wjakob/drjit-nanobind/a.py:23:13 - information: Type of "b" is "A"
/Users/wjakob/drjit-nanobind/a.py:26:13 - information: Type of "c1" is "int"
/Users/wjakob/drjit-nanobind/a.py:30:13 - information: Type of "c2" is "int"
Actual Behavior
a.py:20: note: Revealed type is "a.B"
a.py:23: note: Revealed type is "a.A"
a.py:26: note: Revealed type is "builtins.int"
a.py:29: error: Argument 1 to "get" has incompatible type "B"; expected "Container[Container[int]]" [arg-type]
a.py:30: note: Revealed type is "builtins.int"
Found 1 error in 1 file (checked 1 source file)
Your Environment
Python 3.12.2, MyPy 1.8.0
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione a.py dell’issue, usando Python 3.12.2 e MyPy 1.8.0, e confrontala con l’esempio MyPy-Play collegato. Analizza l’inferenza annidata di TypeVar per get(get(a)). Il lavoro è completato quando la chiamata annidata non produce alcun errore incompatible-argument e c2 viene rivelato come builtins.int, come c1.
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
- 45/100