Cannot infer generic parameter types within `match` statements
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Related to #13612.
#13618 fixed the error reported in #13612, so mypy correctly infers the type of the unpacked value.
However, mypy does not correctly infer the type parameter of the class containing the generic attribute within the scope of the match statement, despite correct inference outside the statement.
To Reproduce
Extending the reprex from #13612:
from typing import Generic, TypeVar
T = TypeVar("T")
class A(Generic[T]):
x: T
__match_args__ = ("x",)
def __init__(self, x: T):
self.x = x
a = A("foo")
reveal_type(a) # A[builtins.str] (correct)
reveal_type(a.x) # builtins.str (correct)
match a:
case A(x) as a_match:
reveal_type(x) # builtins.str (correct)
reveal_type(a_match) # A[Any] **(incorrect! should be A[str])**
Expected Behavior
Within the match statement, A(x) should be inferred to be of type A[str].
Revealed type is "test_generic.A[builtins.str]"
Revealed type is "builtins.str"
Revealed type is "builtins.str"
Revealed type is "test_generic.A[builtins.str]"
I have found that pyright, or at least the pylance VS code extension, infers this correctly. See below:
Actual Behavior
The final line does not reveal A[str].
Revealed type is "test_generic.A[builtins.str]"
Revealed type is "builtins.str"
Revealed type is "builtins.str"
Revealed type is "test_generic.A[Any]"
Your Environment
- Mypy version used: 1.14.1
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.12.8
Motivation
This becomes relevant when trying to match on a union of generic types, as it creates an unreachable error.
e.g.
type Union_AB[T] = A[T] | B[T]
def func(val: Union_AB[int]) -> None:
match val:
case A(x):
pass
case B(x): # error: Subclass of "A[int]" and "B[Any]" cannot exist: would have incompatible method signatures [unreachable]
pass
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
Führe zuerst den bereitgestellten generischen match-Reproducer mit mypy 1.14.1 aus und vergleiche die aufgedeckten Typen mit der erwarteten Ausgabe. Verfolge mypy's Inferenz beim Pattern Matching für den erfassten a_match-Wert und füge anschließend eine Regression-Abdeckung für generische Klassenmuster hinzu. Bestätige, dass das Union-Beispiel den inkorrekten Fehler bezüglich Unerreichbarkeit nicht mehr meldet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100