python / python/mypy

Don't require annotations of kwargs initialized to bool, float, int or string constants

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

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

feature
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

See PR: #12918 (draft).

Feature

There should be no need for annotations for kwargs in the following (partial) function definitions:

def bool_args(ok=True, die=False)
def int_arg(i=1)
def float_arg(ratio=1.0)
def string_arg(s="Hello World")

In this example, the natural annotations are as follows:

def bool_args(ok: bool=True, die: bool=False): pass
def int_arg(i:int=1): pass
def float_arg(ratio: float=1.0): pass
def string_arg(s:str="Hello World"): pass

The issue proposes a non-trivial change to mypy's type inference. The proposal suggests that mypy act as if a natural annotation exists for an initialized kwarg. Note: mypy will (must!) check the new (natural) annotation, and mypy must complain if the natural annotation is not correct.

Pitch

At present, with disallow_untyped_defs = True and disallow_incomplete_defs = False, mypy complains that the signatures above lack annotations for the kinds of kwargs shown above. Such annotations unnecessarily clutter code. It should be possible to infer the types of the kwargs based on the obvious types of the constants!

This feature would fix my biggest gripe about mypy :-)

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

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

はじめの一歩

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

調査の方向性

まずドラフト PR #12918 と、bool、int、float、string の定数で初期化されるキーワード引数に対する自然なアノテーションの issue の例を確認します。関連する型推論の動作を追跡し、推論されたアノテーションの正しさが検証されることを確認します。明示的なアノテーションなしでこれらのケースが disallow_untyped_defs を満たし、無効なデフォルト値では引き続きエラーが生成されれば完了です。

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

評価

技術スタック
python
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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