Generic type inferred wrong; curious difference betweer `aiter(f(x))` and `c = f(x); aiter(c)`
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 42/100
Direzione di ricerca
Inizia con il reproducer main.py dell’issue, usando le versioni di Python e mypy indicate, e confronta i casi aiter(coro_to_aiter(coro)) separato e annidato. Traccia il modo in cui l’inferenza dei tipi generici gestisce ogni chiamata e aggiungi o aggiorna un test di regressione mirato per la discrepanza. Il lavoro è completato quando entrambe le forme ricevono diagnostiche coerenti e previste.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Bug Report
Consider this code (playground):
from typing import TypeVar, Generic, Callable, Coroutine, Any, AsyncIterator
_T = TypeVar("_T")
class StartedToken:
pass
class ProducerContext(Generic[_T]):
pass
async def coro_to_aiter(
coroutine: Callable[[ProducerContext[_T]], Coroutine[Any, Any, None]]
) -> AsyncIterator[_T | StartedToken]:
yield StartedToken()
async def coro(ctx: ProducerContext[int]) -> None:
pass
# Split into two lines, no error:
c = coro_to_aiter(coro) # no error
it = aiter(c) # no error
# The same on one line -> mypy error:
it2 = aiter(coro_to_aiter(coro)) # error: Argument 1 to "coro_to_aiter" has incompatible type ...
Expected Behavior
I would expect mypy to not error on the it2 definition line. Moreover, regardless of whether mypy can handle these definitions, I would expect to see the same behavior (error or absence of error) in the "Split into two lines" case and the "on one line" case.
Actual Behavior
I get this error on the it2 line, and no errors on other lines:
main.py:24: error: Argument 1 to "coro_to_aiter" has incompatible type "Callable[[ProducerContext[int]], Coroutine[Any, Any, None]]"; expected "Callable[[ProducerContext[int | StartedToken]], Coroutine[Any, Any, None]]" [arg-type]
Your Environment
- Mypy version used: 1.6.1 (playground), current master
- Python version used: 3.11, 3.12
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Merge medio
- 1g 18h
- PR unite (30g)
- 54
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.
Altre issue di python/mypy
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
bug topic-configuration topic-error-reporting
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
zostera/django-bootstrap4#894 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
use-agent-os/agent-os#3276 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
NousResearch/hermes-agent#117848 ·