[2.0 regression] Bad result from heuristic non-narrowing of type[T]
Offen
Dieses Issue hat noch niemand übernommen.
bug
topic-type-narrowing
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
(A clear and concise description of what the bug is.)
To Reproduce
from math import isnan
from typing import TypeVar
V = TypeVar("V")
def assert_scalar_equal(s1: V, s2: V, /) -> None:
assert type(s1) is type(s2)
if type(s1) is float and type(s2) is float and isnan(s2):
assert isnan(s1)
else:
assert s1 == s2
Expected Behavior
This snippet should type check.
Actual Behavior
mypy.ini: No [mypy] section in config file
t.py:9: error: Argument 1 to "isnan" has incompatible type "V"; expected "SupportsFloat | SupportsIndex" [arg-type]
t.py:10: error: Argument 1 to "isnan" has incompatible type "V"; expected "SupportsFloat | SupportsIndex" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
Even more minimal reprod:
from math import isnan
from typing import TypeVar
V = TypeVar("V")
def foo(s1: V, /) -> None:
if type(s1) is float:
assert isnan(s1)
mypy.ini: No [mypy] section in config file
t.py:9: error: Argument 1 to "isnan" has incompatible type "V"; expected "SupportsFloat | SupportsIndex" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 2.0.0 or 2.1.0
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: tested 3.11.14 and 3.14.6
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
Beginne damit, die minimale t.py-Reproduktion mit mypy 2.0.0 oder 2.1.0 auszuführen und das Verhalten der Typ-Eingrenzung nach type(s1) is float zu untersuchen. Vergleiche es mit der erwarteten Eingrenzung für isnan; als erledigt gilt die Aufgabe, wenn beide bereitgestellten Snippets ohne die gemeldeten arg-type-Fehler die Typprüfung bestehen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 55/100