Awaitable typeguards
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
Currently it seems impossible to have async TypeGuard or TypeIs:
from typing_extensions import TypeIs
async def foo(a: int | str) -> TypeIs[int]:
raise NotImplementedError
async def main() -> None:
a: int | str = 10
if await foo(a):
reveal_type(a)
else:
reveal_type(a)
Current specification is not clear about how async functions returning TypeIs or TypeGuards should be treated, but I believe this would be useful for async lazy evaluation of typestate fields, example:
from typing_extensions import TypeIs, TypeVar, Generic
T = TypeVar('T', int | None, int, None, covariant=True)
class A(Generic[T]):
_a: T
async def get_a(self: A[int | None]) -> int | None:
a = 123 # imagine some expensive async operation, e.g. fetch value from API
self._a = a
return self._a
def method_requires_a(self: A[int]) -> None:
raise NotImplementedError
def __init__(self):
self._a = None
async def has_a(a: A[int | None]) -> TypeIs[A[int]]:
return await a.get_a() is not None
async def main(a: A[int | None]):
if await has_a(a):
a.method_requires_a() # currently fails as type was not narrowed
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
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
Das Issue nennt keine Repository-Dateien, Tests oder Einstiegspunkte für die Implementierung. Beginne damit, die Behandlung asynchroner Funktionen, die TypeIs oder TypeGuard zurückgeben, in der Typing-Spezifikation zu prüfen, und bestimme anschließend, wie das vorgeschlagene Narrowing definiert werden sollte; als abgeschlossen gilt die Aufgabe, wenn eine abgestimmte Spezifikation sowie die entsprechende Implementierung und Tests vorliegen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100