[@overload]: What is the effect of the *implementation* type-signature?
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 1.8k
- Forks
- 302
- Merge medio
- 23 h
- PR fusionados (30 d)
- 8
Descripción
One thing that I've found underspecified when using @overload is what the type signature of the actual function should be.
For example, let's say I have a function like:
from typing import overload, Literal
@overload
def f(x: int, z: Literal[True]) -> str: ...
@overload
def f(x: int, z: Literal[False] = ...) -> int: ...
def f(x: int, z: bool = False) -> str | int:
if z:
return "hello"
return 1
What is the effect of the last line? In particular:
- The documentation and examples often just use no annotations for the implementation signature (
def f(x, z):). Is that the right thing to do (I sometimes getOverloaded implementation is not consistent with signature of overload 1errors if I leave out the type annotations)? - If I do specify types on the last, do I also have to add it to
@overloadlist -- in other words:
@overload
def f(x: int, z: Literal[True]) -> str: ...
@overload
def f(x: int, z: Literal[False] = ...) -> int: ...
@overload
def f(x: int, z: bool = ...) -> str | int # is this overload necessary?
def f(x: int, z: bool = False) -> str | int:
if z:
return "hello"
return 1
Both pyright and mypy seem to interpret things differently with and without it (e.g. see this play link).
I understand that @overload is a complicated feature, but I'm hoping this is a small corner we can start with.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
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
Comienza con los ejemplos de @overload de este issue y ejecútalos en el Pyright playground enlazado; después, compara los resultados con mypy. Revisa cómo interpretan ambas herramientas las firmas de implementación y las listas de overload. El trabajo se considera terminado cuando se haya alcanzado una explicación consensuada de la semántica y se hayan documentado el comportamiento esperado y los ejemplos.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- documentation
- Tipo de issue
- Documentación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 25/100