Union of Literals as key in union of TypedDict false positive
まだ誰も着手していません。
評価
調査の方向性
まず、提供されている Python 3.11 の再現コードを mypy に対して実行し、その後、Literal キーと TypedDict マッピングのユニオンに対する TypedDict キーの検証を追跡します。get_value_from_AB が誤検出なしで型チェックを通過し、単一辞書のケースも引き続き通過すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Bug Report
The function
def get_value(key: CombinedDictKey, mapping: CombinedDict):
return mapping[key]
raises an error when mapping is a union of two TypedDicts and key is a union of two Literals containing the keys of those TypedDicts. It works when typed with just one of the Literals and TypedDicts.
To Reproduce
from typing import Literal, TypedDict, Union
class DictA(TypedDict):
x: int
y: int
class DictB(TypedDict):
z: str
class DictAB(DictA, DictB):
pass
DictAKey = Literal["x", "y"]
DictBKey = Literal["z"]
DictABKey = Union[DictAKey, DictBKey]
dict_a = DictA(x=1, y=2)
dict_b = DictB(z="z")
dict_ab = DictAB(**dict_a, **dict_b) # type: ignore # Unrelated issue #11108
def get_value_from_A(key: DictAKey, mapping: DictA):
return mapping[key] # Passes type check as expected
def get_value_from_B(key: DictBKey, mapping: DictB):
return mapping[key] # Passes type check as expected
def get_value_from_AB(key: DictABKey, mapping: DictAB):
return mapping[key] # Fails type check with error: TypedDict key must be a string literal
Expected Behavior
return mapping[key] passes typing in all three cases.
Actual Behavior
In the case of get_value_from_AB, return mapping[key] raises error: TypedDict key must be a string literal; expected one of ("z", "x", "y"). This is unexpected as it passes for the functions typed with only one of DictA or DictB.
Your Environment
- Mypy version used: 0.991
- Python version used: 3.11.0
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 54
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/mypy のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
bug topic-configuration topic-error-reporting
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
bancolombia/sentinel#23 ·
-
test md オープンCI
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
langchain-ai/deepagents#6450 ·
-
bug client
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100