Allow Callable type hint before function definition
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
Feature
Allow
f: Callable[[int], int]
def f(n):
return 42
as an alternative to
def f(n: int) -> int:
return 42
Pitch
Currently, the proposed syntax produces a Name "f" already defined ... [no-redef] error, unlike similar code for non-function values:
# OK
x: int
x = 3
For some complicated function signatures, it would be clearer to provide a single Callable hint than to have argument types
mixed in with the arguments themselves and the return type added after the parameter list. For that matter, the two styles above need not be mutually exclusive:
f: Callable[[int], int]
def f(n: int) -> int:
return 42
mypy should report an error only if the types are identical. (Or perhaps compatible; I don't know if there would be use-cases
for the initial type to be a supertype of the one implied by the function annotations.)
This would be consistent with the def statement being a kind of assignment, as the following is currently allowed:
x: int
x = 3
as well as eliminate the asymmetry between function parameters and "ordinary" names.
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
El issue no indica ningún archivo, prueba ni punto de entrada. Empieza por localizar cómo mypy gestiona las asignaciones anotadas y las definiciones de funciones, y después compara cómo informa de los nombres duplicados. Se considera hecho cuando se acepta la declaración Callable antes de def, las anotaciones compatibles se gestionan de forma coherente y los tipos en conflicto siguen produciendo un error.
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
- Bastante claro
- Aptitud para principiantes
- 25/100