TypeGuard behavior does not match python semantics in async functions
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
I'm not sure if this should go here or in the mypyc issue tracker. I believe its a bug in mypy itself which only causes notable issues when you compile code with mypyc.
Bug Report
TypeGuard behavior does not match python semantics in async functions. The block which a TypeGuard applies to should end at the next await, assuming the TypeGuard is on an instance attribute or some other mutable nonlocal var.
This causes unnecessary TypeErrors in mypyc-compiled code.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=50cf9499e4e5be34d3faa754bd6052c0
Note: This code snippet does not produce any mypy errors, the bug is only exposed when compiling similar code with mypyc.
Expected Behavior
The TypeGuard should only guarantee the type of self._db up to the next await/async with/async for, because after that point the type is no longer guaranteed.
Actual Behavior
The TypeGuard "guarantees" the type of self._db longer than it is logically able to. Mypyc will raise a TypeError on line 12 if another task sets self._db while our coroutine waits for the lock.
Your Environment
- Mypy version used: 1.17.0
- 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 mit dem verknüpften mypy-play-Reproduzierer und vergleiche die TypeGuard-Narrowing vor und nach await, async with und async for. Verfolge das relevante async-function-Typprüfungsverhalten und füge Regressionstests hinzu, die zeigen, dass ein veränderlicher nichtlokaler Wert über den Suspension Point hinaus nicht weiter eingegrenzt wird; als abgeschlossen gilt die Aufgabe, wenn mypyc nicht mehr auf den gemeldeten TypeError stößt.
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
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100