Generic callback protocols break with kwargs
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Ejecuta el reproductor proporcionado del protocolo de callback con el comportamiento reportado de mypy 0.720, comparando los casos sin argumentos, con *args y con **kwargs. Traza las rutas de inferencia de tipos y compatibilidad de callbacks responsables del diagnóstico y añade después una prueba de regresión que muestre que el callback con kwargs se acepta sin perder el tipo de retorno.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100