Set and Dict generalizing the type unnecessarily
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
See these two example functions:
def f(items: list[str] | list[int]) -> None:
items_as_set: set[str] | set[int] = set(items)
def g(key_to_count: dict[str, float] | dict[int, float]) -> None:
key_to_count_2: dict[str, float] | dict[int, float] = {k: v for k, v in key_to_count.items()}
Mypy 1.10.0 complains about the assignment in each of them:
test_mypy_dict.py:2: error: Incompatible types in assignment (expression has type "set[object]", variable has type "set[str] | set[int]") [assignment]
test_mypy_dict.py:5: error: Incompatible types in assignment (expression has type "dict[str | int, float]", variable has type "dict[str, float] | dict[int, float]") [assignment]
To Reproduce
Playground gist: https://mypy-play.net/?mypy=latest&python=3.12&gist=83bde5f3f11470e94b6a680158649641
Expected Behavior
The set() or dict() operation applied on a variable that is either a list[str] | list[int] is always either a set[str] or set[int]. The same applies to function g using a dict. Thus the functions should not raise a mypy error.
Workarounds
I tried two workarounds:
- Using a mixed type of
list[str | int]. However, I get the typelist[str] | list[int]by an external function, thus this is not compatible. - Using a generic. This is not working either.
See the playground gist: https://mypy-play.net/?mypy=latest&python=3.12&gist=264490179628a0a214a2e5bd947b8ccf
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、リンクされている mypy-play の例を含め、issue にある 2 つの Python 再現コードを実行し、set() と dict comprehension について推論された型と代入エラーを比較します。これらのコンストラクター式および comprehension 式に対する型チェックの経路を追跡します。両方の関数がエラーなしで型チェックされ、同種のコンテナー型の union が維持されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers, devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100