![](https://secure.gravatar.com/avatar/c278b7144feb496c731ee0514355548c.jpg?s=120&d=mm&r=g)
On 2/26/21 7:43 PM, Paul Bryan wrote:
Q1. Though the intent is to use in TypedDict, is it necessary to preclude its use elsewhere by making its use outside of TypedDict an error?
Well, what would it mean to have Required[] somewhere else? For example: def identity(x: Required[int]) -> int: ... A type checker is going to have to apply *some* meaning to Required[] used in a parameter type. Should it be ignored? Feels pretty strongly like an error to me that should be flagged. Is there a useful reason to allow Required[] in other positions that I'm not thinking of?
Q2. Can the PEP explicitly address how Required[...] and NotRequired[...] will manifest in TypedDict state? Namely, its effect on __required_keys__ and __optional_keys__?
Sure. Required[] applied to a key means it will definitely appear in __required_keys__ at runtime. Similarly NotRequired[] applied to a key means it will definitely appear in __optional_keys__. Neither __required_keys__ nor __optional_keys__ are mentioned in PEP 589 (TypedDict) [1] or the "typing" module documentation [2] but I think an earlier post to the list suggested that was an oversight and that these runtime-available keys should be documented (presumably in one of those locations) as well. [1]: https://www.python.org/dev/peps/pep-0589/ [2]: https://docs.python.org/3/library/typing.html#typing.TypedDict I wonder if it would be useful to ban confusing combinations of both Required[] and NotRequired[] at the same time, like: class Movie(TypedDict): year: Required[NotRequired[...]] # um; error?; does Required[] win?
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. Thanks for the feedback. -- David Foster | Seattle, WA, USA Contributor to TypedDict support for mypy