mypy doesn't narrow `Final` objects in nested contexts
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
I have a global constant I've marked with Final. In a class definition, I use this constant to swap out implementations of a method. While inside a type guarding block, but outside the method definition, the type of the Final object is narrowed, but inside of the method it is not narrowed.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=2d4c97cffea1dd6710bf5cc7283f7290
from typing import *
def _init() -> None | str:
return None
RESOLVE_X: Final = _init()
class Example:
if RESOLVE_X is not None:
reveal_type(RESOLVE_X) # note: Revealed type is "builtins.str"
def __str__(self) -> str:
return RESOLVE_X # error: Incompatible return value type (got "str | None", expected "str") [return-value]
else:
def __str__(self) -> str:
return "example"
Expected Behavior
Inside the __str__ definition in the if RESOLVE_X is not None: block, RESOLVE_X should stay narrowed as it cannot be reassigned and have its type re-widened in the future.
Actual Behavior
main.py:12: note: Revealed type is "builtins.str"
main.py:15: error: Incompatible return value type (got "str | None", expected "str") [return-value]
Found 1 error in 1 file (checked 1 source file)
Your Environment
Mypy version 1.15.0.
Python 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 mit der im Issue verlinkten mypy-play-Reproduktion und führe sie in der gemeldeten Umgebung mit Python 3.12 und mypy 1.15.0 aus. Verfolge, wie der Type Guard für das module-level Final im Klassenrumpf im Vergleich zur verschachtelten Definition von str behandelt wird. Als erledigt gilt die Aufgabe, wenn die aufgedeckte Eingrenzung innerhalb von str erhalten bleibt und das Beispiel ohne den Return-Value-Fehler durchläuft.
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
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 48/100