Allow for specifying default types based on name
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
My suggestion is to allow for a default_types.py file in the root of a python project. The file should contain a dictonary with the name, type combinations. Which could look like this:
from typing import List
from database import Task
from datetime import date, timedelta
default_types = {
"day": date,
"name": str,
"duration": timedelta,
"task": Task,
"first_task": Task,
"tasks": List[Task],
"task_id": int
}
As a result:
def get_task_name(task_id):
task = Task.get(task_id)
return task.name
would be interpreted as:
def get_task_name(task_id: int):
task: Task = Task.get(task_id)
return task.name
The developer would be able to overwrite the default types explicitly. For example:
def update_task_duration(task, duration: Optional[timedelta]):
if duration is None:
task.duration = task.default_duration
else:
task.duration = duration
Pitch
For a long time I have been irritated about writing type annotations for variables where it as programmer is obvious which types they are, based on its name. It felt like it was discouraging me to make smaller functions because it would lead to more typing.
As a tech lead I want to make my code base consistent and easy to read. I want to encourage developers to give objects good names.
By doing this I fell I could give a very strong incentive to give consistent names and it would result in less uninformative boilerplate and a better developer experience. And it could be adapted very gradually for existing projects one variable name at a time. Where it makes sense for people.
What do you think?
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
Revisa el formato propuesto de default_types.py en el nivel raíz y los ejemplos que muestran anotaciones basadas en nombres y sobrescrituras explícitas. Determina el comportamiento y el alcance previstos para mypy y, después, define cómo se verificaría la finalización para los argumentos de función inferidos, las variables locales y las sobrescrituras de anotaciones explícitas.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- tooling
- 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