python / python/mypy

Allow for specifying default types based on name

Offen
#14,346 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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?

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Überprüfe das vorgeschlagene Format von default_types.py auf der Root-Ebene sowie die Beispiele für namensbasierte Annotationen und explizite Überschreibungen. Bestimme das beabsichtigte Verhalten und den Umfang für mypy und lege anschließend fest, wie die Fertigstellung für inferierte Funktionsargumente, lokale Variablen und explizite Annotation-Überschreibungen überprüft werden soll.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.