python / python/mypy

Specialize empty collections to Any as a fallback

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

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

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

説明

Context

I have a pending PR for typeshed which fixes overparameterized dict.get and dict.pop methods: python/typeshed#10294. The PR causes a lot of failures in various GitHub projects due to how mypy treats empty collections. Specifically, if dict.get is annotated as

class dict(Generic[KT, VT]):
    def get(self, key: KT, default: T) -> T | VT: ...

mypy infers the type of v in

def test(d: dict[str, Any]):
    v = d.get("foo", {})

to be Any | dict[<unknown>, <unknown>], instead of Any | dict[Any, Any].

Feature

Specialize empty collections to Any if no other information is available to recover type arguments.

Pitch

Using <unknown> instead of Any requires users to cast or add type annotations where they otherwise will not be necessary. See comments in the PR for a few examples from real projects.

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

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

はじめの一歩

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

調査の方向性

まず、示されている dict.get の例を mypy で再現し、型引数を復元できない場合に空のコレクション式がどのように推論されるかを調べます。この状況で空のコレクションが unknown ではなく Any に特殊化され、その動作が関連する型チェックテストで引き続きカバーされていれば完了です。

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

評価

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

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

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