python / python/mypy

Allow for specifying default types based on name

Aperta
#14,346 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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?

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Esamina il formato proposto di default_types.py a livello radice e gli esempi che mostrano annotazioni basate sui nomi e override espliciti. Determina il comportamento e l’ambito previsti per mypy, quindi definisci come verificare il completamento per gli argomenti di funzione inferiti, le variabili locali e gli override delle annotazioni esplicite.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.