python / python/mypy

Allow for specifying default types based on name

オープン
#14,346 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

feature
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

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?

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

ルートレベルの default_types.py に提案されている形式と、名前ベースのアノテーションおよび明示的なオーバーライドを示す例を確認してください。mypy に意図されている動作と対象範囲を明確にし、そのうえで、推論された関数引数、ローカル変数、明示的なアノテーションのオーバーライドについて、完了をどのように検証するかを定義してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
tooling
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。