Allow TypedDict.__required_keys__/__optional_keys__ to be used as Literal[...]
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 1.8k
- フォーク
- 302
- 平均マージ
- 23時間
- マージ済み PR(30日)
- 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 にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、TypedDict.required_keys、TypedDict.optional_keys、Literal 間で要求されている相互作用と、mypy および pymongo を使用した例を確認します。この issue ではリポジトリのファイルやテストは指定されていません。完了には、合意された設計と、TypedDict のキー集合が型付きストレージ操作のためにリテラルなキー情報を提供できることの検証が必要です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 25/100