On 2/1/22 3:07 PM, S Pradeep Kumar wrote:
# Type Compatibility with PEP 589 TypedDicts
Could you mention how type compatibility checks work for a PEP-655-style TypedDict against a PEP-589-style TypedDict (and vice versa)? For example, is a single-class Movie with a Required `name` and NotRequired `year` compatible with the inherited `Movie` in your Motivation example?
I'm guessing the answer is "obviously yes", but it's probably worth explicitly laying out in the PEP. [...]
I agree that being explicit RE the interaction with total=False would be valuable. I've added a new "Interaction with total=False" section.
Similarly, is there any difference in runtime or type-checking behavior between a TypedDict with a redundant `name: Required[str]` and one with `name: str`? I'm guessing not but might be worth specifying. Otherwise, there might be questions like, do they generate the same constructor for the Movie class?
There is no difference. I think this is probably clear enough just based on the word "redundant".
Once we specify that they are equivalent for type-checking purposes, type checker implementers (such as me on Pyre) can just desugar the latter to the former without any issues.
Aye, that should be possible to do. However I suspect typecheckers may be able to provide more targeted error messages if they provide some degree of special recognition for the Required[] or NotRequired[] syntax.
# Other comments
A couple of quick readability comments:
**Motivation section**: You've shown the "Before" example. Could you show the "After" example (i.e., the same code using your PEP's feature)? That would make it easy for people who just want to get the gist of the PEP without diving into the Specification.
Agreed. Done.
Would also be great if you can also demonstrate both Required and NotRequired, but this is... not required :)
I'll leave that detail for the Specification section.
**Specification**: Could you nest the three sections for "Interaction with get_type_hints/get_origin/__required_keys" under a "Runtime Behavior" section? That way, users who care just about typing can skip that section.
I had thought about doing that earlier. I'll take your mention as a vote to actually do it. Done.
Is it valid to use `Required` in an inherited TypedDict?
Yes; it is not disallowed. Hopefully the equivalence related to a total=False TypedDict should make this clear implicitly.
**How to Teach This section**:
To define a TypedDict where most keys are required and some are potentially-missing, define a single TypedDict as normal and mark those few keys that are potentially-missing with NotRequired[].
Could you clarify that "define a single TypedDict as normal" means "without a `total` keyword"? Not obvious otherwise.
Done. All of the preceding feedback is pending integration to the PEP in the following PR: -> https://github.com/python/peps/pull/2323 -- David Foster | Seattle, WA, USA Contributor to TypedDict support for mypy