Lambda type cannot be inferred when type context includes a callable with a callable parameter
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
This was filed as a pyright bug. I noticed that mypy likewise doesn't handle this case, and I couldn't find any open issues that cover this.
Mypy playground link
from typing import Callable, Generic, TypeVar
T = TypeVar("T")
def func1(callable: Callable[[Callable[[T], T]], T]) -> T:
return callable(lambda x: x)
v1 = func1(lambda a: a(0))
reveal_type(v1) # Should be int
v2 = func1(lambda a: a(""))
reveal_type(v2) # should be str
class A(Generic[T]):
def __init__(self, value: T) -> None:
self.value = value
def func2(callable: Callable[[type[A[T]]], A[T]]) -> T:
return callable(A).value
v3 = func2(lambda A: A(0))
reveal_type(v3) # Should be int
v4 = func2(lambda A: A(""))
reveal_type(v4) # Should be str
Expected Results: No Errors and revealed types as indicated in the comments
Actual Results: Multiple errors ("Need type annotation for ...").
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
Inizia con il mypy playground collegato ed esegui il riproduttore Python incluso che utilizza Callable, Generic e TypeVar. Confronta i tipi rivelati per v1 fino a v4 con i risultati attesi int e str; il lavoro è completato quando gli esempi non producono errori e rivelano quei tipi.
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
- Specificata chiaramente
- Idoneità per principianti
- 35/100