Allow TypedDict.__required_keys__/__optional_keys__ to be used as Literal[...]
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.8k
- 派生
- 302
- 平均合并
- 23 小时
- 30 天内合并 PR
- 8
描述
TypedDict instances work very well for NoSQL databases, like Mongo DB, but for any extended functionality a class is required, and marrying the two requires a few error-prone steps described here.
Consider a Mongo DB database document described with this structure, which works out well to the most part when being supplied into pymongo methods (with the exception of mypy failing to realize that it's TypedDict is a mutable mapping).
class X(TypedDict):
i: int
s: str
f: NotRequired[float]
Compared to Mapping, using a typed dictionary allows one to catch field references and value type mismatches quite quickly.
For more advanced functionality for this entity, however, a class may be maintained, like below. I will omit getters, etc, and just show relevant methods to set required and optional fields.
class Y:
def __init__(self, x: X) -> None:
self._storage = x
def set_required(self, key: Literal["i", "s"], value: Any) -> None:
self._storage[key] = value
def set_optional(self, key: Literal["f"], value: Any) -> None:
if value is None:
del self._storage[key]
else:
self._storage[key] = value
Herein lies the problem - I cannot use X.__required_keys__ to drive field references in the underlying storage because it's not a literal and the fact that it's frozenset doesn't help here, so I have to maintain copies of field names separately.
If X.__required_keys__ and X.__optional_keys__ would operate similarly to how constexpr behaves in C++ and would propagate literal keys they are created with to where they are used, like those methods above, it would make it very straightforward to integrate typed dictionaries with classes and use the former as storage with a well-defined schema that can be referenced in class methods to enforce proper field references.
I will also note that @dataclass is not good for this functionality because of a couple of reasons. First, just like any class, it has no concept of missing attributes and an attribute set to None is interpreted by the database driver as null. Second, @dataclass is implemented via auto-generated __init__, so it imposes unreasonable field order to satisfy optional arguments following required ones in the constructor. Lack of an alternative constructor doesn't help either.
EDIT: Replaced self.x_o with self._storage, which was a copy-and-paste error copying examples from a larger test case.
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先审查 TypedDict.required_keys、TypedDict.optional_keys 与 Literal 之间所要求的交互,以及使用 mypy 和 pymongo 的示例。该 issue 未指定任何 repository 文件或测试;完成这项工作需要达成一致的设计,并验证 TypedDict 键集合能够为类型化存储操作提供字面量键信息。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100