
On Tue, Mar 2, 2021 at 3:06 PM David Foster <davidfstr@gmail.com> wrote:
[...]
Q3. How will get_type_hints(...), get_options(...) and get_args(...) behave with TypedDicts (and their __annotations__ values) where keys are explicitly marked Required[...] or NotRequired[...]?
Good question. Presumably they would became similarly to how they treat other type qualifiers like Final[] and Annotated[] (whatever that behavior is), but I'll do some further research/thinking here.
Suggestions:
TD = TypedDict("TD", {"a": Required[int]}, total=False)
get_type_hints(TD)
{'a': typing.Required[int]}
a = get_type_hints(TD)["a"]
get_origin(a)
typing.Required
get_args(a)
(<class 'int'>,)
Yes, that's the *preserve* approach.
I could also see an argument that the hints be *erased*, leaving only the requireness information in the `__required_keys__` and `__optional_keys__` collections. Erased types are likely to be easier for user code to work with at runtime.
Again, I'll do some more research RE the behavior of similar Final[] and Annotated[] here when I have some bandwidth.
While Final is not allowed in a TypedDict, in contexts where it is allowed it is preserved. I recommend the same for [Not]Required. -- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>