python / python/mypy

Dict comprehension from Iterable of Literal

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

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

bug false-positive priority-1-normal topic-literal-types
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

Related to #7533
mypy 0.740, python 3.7.4, typing_extensions 3.7.4

dict comprehension won't recognize an Iterable of Literal as input.
Unlike in #7533, this is not limited to just tuples.

The dict constructor and dict.fromkeys have no problem.

from typing import Dict
from typing_extensions import Literal

T = Literal[1, 2, 3]
d: Dict[T, None]

d = {x: None for x in [1, 2, 3]}
# error: Key expression in dictionary comprehension has incompatible type "int"; expected type "Union[Literal[1], Literal[2], Literal[3]]"

d = dict(zip([1, 2, 3], [None, None, None]))  # no error
d = dict.fromkeys([1, 2, 3])  # no error

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

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

はじめの一歩

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

調査の方向性

提供された Python 再現コードから始め、dict comprehension と、動作する dict constructor および dict.fromkeys のケースを比較します。mypy がリテラルキーを Dict[Literal[1, 2, 3], None] に割り当てる comprehension を、互換性のないキー式エラーなしで受け入れれば完了です。

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

評価

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

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

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