python / python/mypy

Allow for specifying default types based on name

Abierto
#14,346 0 comentarios 0 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

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

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

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.