python / python/mypy

TypedDict doesn't support Union[Literal] as key "string literal"

未关闭
#7,752 2 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug priority-2-low topic-literal-types topic-typed-dict topic-union-types
主要语言
Python
星标
20.6k
派生
3.3k
PR 合并指标
PR 指标待抓取

描述

Possibly bug.
Related: https://github.com/python/mypy/issues/7644

Reproducible example

from typing_extensions import Literal, TypedDict

class TDict(TypedDict):
    KEY1: int
    KEY2: int

d1: TDict = {'KEY1': 1, 'KEY2': 2}

key2: Literal['KEY1']
d2: TDict = {key2: 1, 'KEY2': 2}

key3: Literal['KEY1', 'KEY2']
d3: TDict = {key3: 1, 'KEY2': 2}  # error: Expected TypedDict key to be string literal

I use mypy 0.740 and python3.6

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先使用 mypy 0.740 运行提供的 TypedDict 和 Literal 示例,重点关注报告“Expected TypedDict key to be string literal.”的 d3 赋值。当可复现的 union-of-literals 键案例能够与单个 literal 案例一致地被接受时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
devtools
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。