python / python/mypy

UnionType of `Callable[P, Awaitable[T]] | Callable[P, T]` not being matched by async function.

Aperta
#16,646 1 commento 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report
I'm trying to annotate a decorator that can take both async and normal functions using

def decorator(func: Callable[P, Awaitable[T]] | Callable[P, T])
...

The Callable[P, Awaitable[T]] variant does not match when using the decorator with an async function and instead raises an error
"Argument 1 to "decorator" has incompatible type "Callable[[int], Coroutine[Any, Any, str]]"; expected "Callable[[VarArg(Never), KwArg(Never)], Awaitable[Never]] | Callable[[VarArg(Never), KwArg(Never)], Never]" [arg-type]"

This may be related to #16451 which was fixed by 1.7.1

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&flags=new-type-inference&gist=c5255c4185cf954aa32bc8f8b2c04c10
Expected Behavior
Both sync and async should be type matched correctly

Actual Behavior

I get a strange error with the async variant (main.py:61: error: Argument 1 to "decorator" has incompatible type "Callable[[int], Coroutine[Any, Any, str]]"; expected "Callable[[VarArg(Never), KwArg(Never)], Awaitable[Never]] | Callable[[VarArg(Never), KwArg(Never)], Never]" [arg-type])

Your Environment

  • Mypy version used: 1.7.1 compiled
  • Mypy command-line flags: new-type-inference (by default)
  • Python version used: 3.11.6

Also replicated in the playground with 1.7.1 compiled and Python 3.11 and 3.12.

Guida per i contributori

Apri la guida per i contributori

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

Riproduci il report nell'esempio mypy-play collegato usando Python 3.11 e il flag new-type-inference, concentrandoti sull'errore in main.py:61. Traccia il modo in cui il type checker fa corrispondere lo UnionType delle varianti Callable con la funzione async. Il lavoro è completato quando sia l'uso sincrono sia quello asincrono del decoratore superano il controllo dei tipi senza l'errore arg-type segnalato.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.