Generic `NamedTuple` does appear attribute-less in `match` statements, and member type information is erased
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
If a NamedTuple is generic over T, then for some reason, inside a match statement over this NamedTuple, all information about member variables and their types are erased. Even if they are explicitly stated.
To Reproduce
from typing import NamedTuple
class Wrapper[T](NamedTuple):
wrap: T
foo: Wrapper[int] = Wrapper(123)
def bar() -> int:
match foo:
case Wrapper(wrap=w):
return w
case _: # this is only to make this error-less with `pyrefly`
raise RuntimeError("Unreachable")
Expected Behavior
This function clearly matches a Wrapper[int] in its first case. It should detect it can match the internal wrap variable, and it also has to be int.
$ pyrefly check tst.py
INFO 0 errors
$ pyright tst.py
0 errors, 0 warnings, 0 informations
Actual Behavior
mypy for some reason seems to say that Wrapper does not even have a wrap attribute, and also, it's saying that this is Any, even though the matched Wrapper here is Wrapper[int].
$ mypy --strict tst.py
tst.py:12: error: Class "tst.Wrapper[Any]" has no attribute "wrap" [misc]
tst.py:13: error: Returning Any from function declared to return "int" [no-any-return]
Your Environment
- Mypy version used: 1.19.1
- Mypy command-line flags:
--strict - Python version used: 3.12.9
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere den Bericht in tst.py mit Python 3.12.9 und mypy --strict und vergleiche das Ergebnis anschließend wie gezeigt mit pyrefly und pyright. Verfolge, wie das generische NamedTuple-Muster in der match-Anweisung analysiert wird; abgeschlossen ist die Aufgabe, wenn w als das Attribut wrap mit dem Typ int erkannt wird und das Beispiel keine Fehler meldet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 48/100