False positive: Incompatible types in capture pattern (pattern captures type "Exception", variable has type <deleted>)
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
When trying to use a match pattern and bind to a variable with the same name as an except ... as bind, mypy starts returning an error.
If I remove the try/except block or don't try to attempt to reassign the variable, the issue goes away.
I've tried to search for other similar issues, but couldn't find much. Hope it's not a duplicate.
To Reproduce
def main() -> None:
try:
pass
except Exception as exc:
print(exc)
result = Exception() # this could for example be the result of `asyncio.gather(..., return_exceptions=True)`
match result:
case Exception() as exc:
print("got exception", exc)
case _:
print("got something else", result)
main()
Gist URL: https://gist.github.com/mypy-play/4d9bc9f42141eb6093823efcbc0dafd7
Playground URL: https://mypy-play.net/?mypy=master&python=3.13&gist=4d9bc9f42141eb6093823efcbc0dafd7&flags=allow-redefinition
Tried with various versions of mypy (1.8.0, 1.15.0, master) and Python (3.10, 3.11, 3.13).
Tried with --allow-redefinition and without.
Expected Behavior
No error from mypy, at least when using --allow-redefinition.
Actual Behavior
main.py:9: error: Incompatible types in capture pattern (pattern captures type "Exception", variable has type <deleted>) [misc]
main.py:10: error: Trying to read deleted variable "exc" [misc]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.8.0, 1.15.0, master
- Mypy command-line flags: --allow-redefinition
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.10, 3.11, 3.13
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 Reproduktion in main.py und führe mypy mit und ohne --allow-redefinition aus. Verfolge das Zusammenspiel zwischen der Bindung durch except ... as und dem späteren match-Capture-Muster für exc. Die Aufgabe ist erledigt, wenn die Reproduktion nicht mehr die Fehler wegen inkompatibler Captures oder gelöschter Variablen meldet und es eine Regressionstestabdeckung für diesen Fall gibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100