Unions of Literals are not accepted as TypedDict Keys
还没有人认领这个 Issue。
评估
调研方向
先从链接的 Mypy Play 示例中 minimal_fail 和 check_ranges 的复现开始,然后将它们与 minimal_okay 进行比较。当字面量类型的并集可以作为 TypedDict 键被接受,且不会出现 literal-required 错误(包括嵌套并集的情况)时,此更改即完成。
由索引模型根据 Issue 内容生成。
描述
Bug Report
If Literals are given as Unions they aren't accepted as TypedDict Keys
To Reproduce
from typing import TypedDict, reveal_type, Final, Literal, TypeAlias, get_args
from datetime import datetime
class RangeInfo(TypedDict):
begin: datetime
end: datetime
created_start: datetime
created_end: datetime
# …
TimeRange: TypeAlias = Literal["begin", "end"]
CreatedRange: TypeAlias = Literal["created_start", "created_end"]
RANGES: Final[tuple[tuple[TimeRange | CreatedRange, ...],... ]] = (
get_args(TimeRange),
get_args(CreatedRange),
# … much more range names
)
def check_ranges(option: RangeInfo) -> None:
"""Ensure given datetime ranges are valid"""
for range_tuple in RANGES:
reveal_type(range_tuple) # builtins.tuple[Union[Union[Literal['begin'], Literal['end']], Union[Literal['created_start'], Literal['created_end']]], ...]
for range_val in range_tuple:
reveal_type(range_val) # Union[Union[Literal['begin'], Literal['end']], Union[Literal['created_start'], Literal['created_end']]]
if not isinstance(option[range_val], datetime): # ❌ TypedDict key must be a string literal; expected one of ("begin", "end", "created_start", "created_end") [literal-required]
raise ValueError(f"{range_val} is not a datetime")
# … much more checks
def minimal_okay(union: Literal["begin"] | Literal["end"], option: RangeInfo) -> datetime:
return option[union]
def minimal_fail(union: TimeRange | CreatedRange, option: RangeInfo) -> datetime:
return option[union] # ❌ TypedDict key must be a string literal; expected one of ("begin", "end", "created_start", "created_end") [literal-required]
Expected Behavior
There should be no type error, Literals in Unions should be flattened.
I would love if unions of literals are flattened in general.
Literal["a"] | Literal["b"] | (Literal["c"] | Literal["d"]) == Literal["a", "b", "c", "d"]
But I guess this is yet another issue.
Actual Behavior
Can't access typed dict without type error
Your Environment
(see mypy play)
Related #16813
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- 平均合并
- 1 天 18 小时
- 30 天内合并 PR
- 54
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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
-
bug
难度 2/5 1-3 小时 新手友好度 86/100
zostera/django-bootstrap4#894 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
use-agent-os/agent-os#3276 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
难度 2/5 1-3 小时 新手友好度 88/100
NousResearch/hermes-agent#117848 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
zilliztech/memsearch#759 ·