Cannot turn existing dictionary into TypedDict
还没有人认领这个 Issue。
评估
调研方向
首先使用 issue 中的 TypedDict 示例重现该诊断,并检查 mypy 的 TypedDict 构造函数检查。将被接受的字面量和 dict(...) 形式与被拒绝的现有字典形式进行比较;当预期支持的转换能够按照与运行时行为一致的方式进行类型检查,并由测试覆盖时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
I expected any of these forms to be allowed by MyPy, but they aren't:
# typed_dict_test.py
from typing import TypedDict
class D(TypedDict):
a: str
b: int
d_untyped = {"a": "foo", "b": 1}
d0 = D({"a": "foo", "b": 1}) # The only one that works
print(d0)
d1 = D(d_untyped)
print(d1)
d2 = D(**d_untyped)
print(d2)
d3 = D(dict(d_untyped))
print(d3)
d4 = D(dict(**d_untyped))
print(d4)
$ python typed_dict_test.py
{'a': 'foo', 'b': 1}
{'a': 'foo', 'b': 1}
{'a': 'foo', 'b': 1}
{'a': 'foo', 'b': 1}
{'a': 'foo', 'b': 1}
$ python -V
Python 3.8.3
$ mypy typed_dict_test.py
typed_dict_test.py:13: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor [misc]
d1 = D(d_untyped)
^
typed_dict_test.py:16: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor [misc]
d2 = D(**d_untyped)
^
typed_dict_test.py:19: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor [misc]
d3 = D(dict(d_untyped))
^
typed_dict_test.py:22: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor [misc]
d4 = D(dict(**d_untyped))
^
Found 4 errors in 1 file (checked 1 source file)
$ cat mypy.ini
[mypy]
warn_redundant_casts = True
warn_unused_configs = True
pretty = True
show_error_codes = True
disallow_any_generics = True
disallow_subclassing_any = True
disallow_untyped_calls = True
disallow_incomplete_defs = True
check_untyped_defs = True
disallow_untyped_decorators = True
no_implicit_optional = True
warn_unused_ignores = True
warn_return_any = True
no_implicit_reexport = True
I checked that cast(D, ...) works, but I wonder if this is the right way to proceed, or a workaround.
- 主要语言
- 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
-
area/auth bug comp/agent P3 platform/discord type/security
难度 2/5 1-3 小时 新手友好度 88/100
NousResearch/hermes-agent#117848 ·
-
难度 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