python / python/mypy

How to inherit argument types from a specific wrapped function

Abierto
#13,617 1 comentario 5 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

It would be great if there were a way to inherit the (argument) type signatures from one function to another. The use case here is something like:

def foo(
    something: str,
    *args: *requests.get.args,
    **kwargs: **requests.get.kwargs
):
    print(something)
    return requests.get(*args, **kwargs)

I've found https://github.com/python/mypy/issues/2003, though that was closed a while back. I'm not totally clear why that was closed (from the comments I can't work out what the solution was, nor if agreement was reached on an approach). Additionally a lot has changed since then with regards to argument typing -- notably ParamSpec, TypeVarTuple & Unpack now exist, though I'm not sure if any of them actually help here as the wrapped function is static rather than also being part of the call signature.

Ideally I'd like to be able to do this in cases where the original function is annotated, though without needing to change that function's annotations (e.g: because it's in a library), so using e.g: Unpack[TypedDict] would require me to repeat the definitions of the original function's signature, which is partly what I'm trying to avoid by using passthrough *args, **kwargs in the first place.

Aside: is "inherit" the right term for this? I wasn't sure what to search for.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza revisando la sintaxis propuesta en este issue y la discusión previa en mypy issue #2003. Compara si ParamSpec, TypeVarTuple o Unpack permiten heredar anotaciones de una función envuelta estáticamente como requests.get; para darlo por hecho sería necesario un diseño acordado y un comportamiento documentado del comprobador de tipos.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
devtools
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.