TypeIs and TypeGuard fail to narrow type[C]
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
A TypeIs/TypeGuard function fails to narrow a type[A] expression. A plain if guard block treats the branch as unreachable instead. A comprehension filter keeps the unnarrowed type instead.
To Reproduce
from typing_extensions import TypeIs
class A: ...
class M: ...
class C(M, A): ...
def is_m(t: type) -> TypeIs[type[M]]:
return issubclass(t, M)
cls: type[A] = C
if is_m(cls):
reveal_type(cls)
Expected Behavior
reveal_type(cls) reports type[<subclass of "A" and "M">].
Actual Behavior
Success: no issues found in 1 source file
No reveal_type note at all -- the branch is silently treated as unreachable.
Second repro (comprehension)
from typing import Type
from typing_extensions import TypeIs
class A: ...
class M: ...
class B(A): ...
class C(M, A): ...
def is_m(t: type) -> TypeIs[Type[M]]:
return issubclass(t, M)
alist: list[Type[A]] = [B, C]
mlist: list[Type[M]] = [cls for cls in alist if is_m(cls)]
reveal_type(mlist)
Expected Behavior
No error, mlist is list[type[M]].
Actual Behavior
error: List comprehension has incompatible type List[type[A]]; expected List[type[M]] [misc]
Environment
- Mypy version used: mypy 2.2.0+dev (main branch)
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): nothing special - Python version used: 3.12
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
Es wird keine Projektdatei und kein Test genannt. Beginne damit, die Tests und die Implementierung zur Typverengung nach TypeIs, TypeGuard, bedingten Verzweigungen und Filtern in Comprehensions zu durchsuchen, und führe dann beide Reproduktionen mit mypy aus. Fertig ist es, wenn der erste reveal_type den erwarteten Schnittmengentyp meldet und der zweite list[type[M]] ohne Fehler ableitet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 55/100