An instance of a final class should be treated as satisfying a TypeVar bound to it, after being narrowed
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
See title, I think this is the cause of the remaining issue for https://github.com/python/mypy/issues/9424. I think this is a followup to https://github.com/python/mypy/pull/19183.
To Reproduce
from typing import TypeVar, final
@final
class A:
pass
U = TypeVar('U', bound=A | int)
def f(u: U) -> U:
if isinstance(u, A):
return A()
else:
return u
Expected Behavior
No errors
Actual Behavior
There's an error:
main.py:11: error: Incompatible return value type (got "A", expected "U") [return-value]
Found 1 error in 1 file (checked 1 source file)
Your Environment
Reproduced on mypy play.
- Mypy version used: v1.16
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files): N/A - 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
Beginne damit, das bereitgestellte Python-Beispiel mit mypy v1.16 unter Verwendung des mypy playground oder eines lokalen Checkouts auszuführen. Untersuche, wie das Narrowing einer Instanz der finalen Klasse A mit dem an A | int gebundenen TypeVar U interagiert. Erledigt ist die Aufgabe, wenn das Beispiel ohne den Fehler wegen eines inkompatiblen Rückgabewerts eine Typprüfung besteht.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100