Making a variable optional in an `else` block doesn't work for `try`/`else`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- Ø Merge
- 1 T. 18 Std.
- Gemergte PRs (30 T.)
- 54
Beschreibung
Bug Report
#11002 allowed making a variable optional in an else block; this appears not to work in an else block from a try.
To Reproduce
# t.py
def __() -> None:
try:
...
except ValueError as exc:
e = exc
else:
e = None
$ mypy t.py
t.py:7: error: Incompatible types in assignment (expression has type "None", variable has type "ValueError")
Found 1 error in 1 file (checked 1 source file)
Expected Behavior
I expect this to pass, as it would if it were an if/else
Actual Behavior
As above.
Your Environment
- Mypy version used: 0.950
- Mypy command-line flags: n/a
- Mypy configuration options from
mypy.ini(and other config files): n/a - Python version used: Python 3.10.4 (main, Apr 26 2022, 19:42:59) [Clang 13.1.6 (clang-1316.0.21.2)]
- Operating system and version: MacOS 12.3.1 (21E258)
I'm not sure if this is expected, and try/except was out of scope for the original feature, or whether this is a bug.
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 gemeldete t.py-Beispiel mit mypy 0.950 auszuführen, und vergleiche sein try/except/else-Verhalten mit dem in issue #11002 beschriebenen if/else-Verhalten. Verfolge den Type-Checking-Pfad für Zuweisungen über try-Blöcke hinweg. Füge anschließend einen Regressionstest für das Beispiel hinzu und bestätige, dass mypy die None-Zuweisung akzeptiert, ohne Fehler einzuführen.
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
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100