Can't use Callable[..., T] | Callable[..., Awaitable[T]] as a function argument
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Code:
I was trying to define a callable that can be async or not and use a TypeVar as the return type, but it seem to be broken, here's the full example:
from typing import Union, Awaitable, Callable, TypeVar, Any
T = TypeVar("T")
_RESOLVER_TYPE = Union[
Callable[..., T],
Callable[..., Awaitable[T]],
]
def x() -> int:
return 1
async def a_x() -> int:
return 1
def field_ok(
resolver: _RESOLVER_TYPE,
) -> Any:
...
def field_broken(
resolver: _RESOLVER_TYPE[T],
) -> Any:
...
field_ok(x)
field_ok(a_x)
field_broken(x)
field_broken(a_x)
if I don't pass T it seem to work, no sure what is wrong, the error is:
main.py:31: error: Argument 1 to "field_broken" has
incompatible type "Callable[[], Coroutine[Any, Any, int]]";
expected "Union[Callable[..., <nothing>], Callable[..., Awaitable[<nothing>]]]" [arg-type]
Here's the playground url: https://mypy-play.net/?mypy=latest&python=3.11&gist=6d8ed4a4e55e0d6b2712eff23cd3e3b0
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci l’esempio da main.py usando il mypy playground collegato, confrontando field_ok e field_broken con callable sia sincrone sia asincrone. Traccia come viene interpretata la Union parametrizzata di Callable e Awaitable e considera il problema risolto quando l’esempio non segnala più l’errore incompatible-type per nessuna delle due chiamate.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100