python / python/typing

Awaitable typeguards

Aperta
#1,872 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic: feature
Lingua principale
Python
Stelle
1.8k
Fork
302
Merge medio
23h
PR unite (30g)
8

Descrizione

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

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

L’issue non indica file del repository, test o punti di ingresso dell’implementazione. Inizia esaminando il trattamento, nella specifica di typing, delle funzioni asincrone che restituiscono TypeIs o TypeGuard, quindi determina come dovrebbe essere definito il narrowing proposto; il lavoro sarà considerato completato quando saranno disponibili una specifica concordata e l’implementazione e i test corrispondenti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.