Generic callback protocols break with kwargs
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
I'm getting an unexpected error when using callback protocols with kwargs
Normal callback with no args or kwargs works fine:
from typing import *
T1 = TypeVar('T1')
T2 = TypeVar('T2')
T_contra = TypeVar('T_contra', contravariant=True)
T_co = TypeVar('T_co', covariant=True)
class Callback(Protocol[T_contra, T_co]):
def __call__(self, __element: T_contra) -> T_co:
pass
def applyit(func: Callback[T1, T2], y: T1) -> T2:
return func(y)
def callback(x: str) -> int:
pass
applyit(callback, 'foo') # OK!
callback with args works fine:
from typing import *
T1 = TypeVar('T1')
T2 = TypeVar('T2')
T_contra = TypeVar('T_contra', contravariant=True)
T_co = TypeVar('T_co', covariant=True)
class CallbackWithArgs(Protocol[T_contra, T_co]):
def __call__(self, __element: T_contra, *args: Any) -> T_co:
pass
def applyit_with_args(func: CallbackWithArgs[T1, T2], y: T1) -> T2:
return func(y)
def callback_with_args(x: str, *args: Any) -> int:
pass
applyit_with_args(callback_with_args, 'foo') # OK!
But the same thing with kwargs produces an error:
from typing import *
T1 = TypeVar('T1')
T2 = TypeVar('T2')
T_contra = TypeVar('T_contra', contravariant=True)
T_co = TypeVar('T_co', covariant=True)
class CallbackWithKwargs(Protocol[T_contra, T_co]):
def __call__(self, __element: T_contra, **kwargs: Any) -> T_co:
pass
def applyit_with_kwargs(func: CallbackWithKwargs[T1, T2], y: T1) -> T2:
return func(y)
def callback_with_kwargs(x: str, **kwargs: Any) -> int:
pass
# Argument 1 to "applyit_with_kwargs" has incompatible type "Callable[[str, KwArg(Any)], int]"; expected "CallbackWithKwargs[str, <nothing>]"
applyit_with_kwargs(callback_with_kwargs, 'foo') # Error!
Perhaps this is expected behavior and I'm just overlooking some subtle interplay between the args in the last case, but it seems like a bug.
mypy 0.720
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
Esegui il riproduttore fornito del protocollo di callback con il comportamento segnalato di mypy 0.720, confrontando i casi senza argomenti, con *args e con **kwargs. Traccia i percorsi di inferenza dei tipi e di compatibilità dei callback responsabili della diagnostica, quindi aggiungi un test di regressione che mostri che il callback con kwargs viene accettato senza perdere il tipo di ritorno.
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