-
Notifications
You must be signed in to change notification settings - Fork 258
Allow TypedDict.__required_keys__/__optional_keys__ to be used as Literal[...] #1394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Are you saying that you want to use the variable You mentioned a couple of problems with using |
No. That's the difference between a requirement and an implementation. I'm saying that it would good to have functionality that would behave as if
I'm aware of possible workarounds that can be applied to |
I don't understand what you mean by this statement. Can you elaborate? What would the resulting code look like if this worked the way you have in mind? |
I don't have the exact syntax to suggest, if that's what you are asking. That would be new functionality to design, which would produce the equivalent of This is technically possible because instances of Functionally, it would behave as if this was true: assert(Literal["i", "s"] == Literal[X.RequireKeysFinal]) Note, though, I'm not suggesting this syntax. Just conveying the thought on how it would functionally behave. |
class Foo(TypedDict):
x: int
y: str
def extract_field(d: Foo, field_name: Literal[Foo.__required_keys__]):
...
data: Foo = {"x": 2, "y": "hi"}
extract_field(data, "x") # Good
extract_field(data, "y") # Good
extract_field(data, "z") # Type checker error: z is not present in Literal[Foo. __required_keys__]
s: str = ...
extract_field(data, s) # Type checker error: str is not compatible with Literal[Foo. __required_keys__] I'm guessing something like this is what you are requesting? |
@hmc-cs-mdrissi Thanks for responding Yes, something along these lines would allow people to reuse typed dictionary keys as literal sets and have
|
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 ofmypy
failing to realize that it'sTypedDict
is a mutable mapping).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.
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'sfrozenset
doesn't help here, so I have to maintain copies of field names separately.If
X.__required_keys__
andX.__optional_keys__
would operate similarly to howconstexpr
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 toNone
is interpreted by the database driver asnull
. 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
withself._storage
, which was a copy-and-paste error copying examples from a larger test case.The text was updated successfully, but these errors were encountered: