python / python/mypy

Disallow assignment of `Any` to known/explicit types and/or Introduce `Unknown` type

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

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

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

説明

Feature

I'd like an option that prevents assigned of the Any type to something with a known type, in particular places where I've specified the type.

Returns:

def return_type_a() -> TypeA:
   untyped = {}
   return untype  # Disallow

Assignement:

store : Dict[StoreRef,StoreItem = {}

def create() -> Dict:
  return {}

store = create()  # Disallow

Pitch

It'd be nice to have a stricter boundary between typed and untyped code. The samples I show above are frequent sources of error for me and they quietly slip by the type checking.

While migrated code there are still a lot of Any's in the code, and in some domains they'll never go away (when dealing with genuinely generic data structures, also now since some recrusive types, like JSON, need to use Any).

Related

An explicit Unknown type, in contrast to Any, would also help a lot. TypeScript has an unknown and it is helpful for dealing with generic data and not accidentally using it as the wrong type.

If an Unknown was available the above could also be achieved by turning on the "disallow any" flags and using Unknown in places where generic data is needed.

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

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

はじめの一歩

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

調査の方向性

まず、戻り値と代入の例を動作上のケースとして使い、mypy における Any の扱いと既存の disallow-any flags を読みます。この作業の対象が、より厳格な代入チェック、Unknown 型、またはその両方のどれなのかを明確にします。選択した動作が仕様化され、正当なジェネリックデータの利用を壊すことなく強制されていれば完了です。

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

評価

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

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

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