This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Zac Hatfield-Dodds
Recipients Zac Hatfield-Dodds, levkivskyi
Date 2019-11-18.00:53:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Consider the following cases:

class A(typing.TypedDict):
    a: int  # a is required

class B(A, total=False):
    b: bool  # a is required, b is optional

class C(B):
    c: str  # a is required, b is optional, c is required again

PEP-589 is clear about the semantics, and this is obvious enough when reading the code.  At runtime the __annotations__ attribute of each class gives us the set of allowed keys and the type of each corresponding value, but we have a problem:

- C has __total__==True, but b is not actually required.
- B has __total__==False, but a *is* required.
- I can't see any way to get the parent classes of a TypedDict class!

The _TypedDictMeta metaclass updates the attributes, but leaves no record of the parent type - at runtime A, B, and C all appear to inherit directly from dict.

After discussion on the typing-sig mailing list, I propose to add __required_keys__ and __optional_keys__ attributes to TypedDict subclasses, as frozensets of strings.

This will be very useful for Hypothesis' `from_type()` strategy, as well as for type-based validation frameworks like pydantic or typeguard.
Date User Action Args
2019-11-18 00:53:55Zac Hatfield-Doddssetrecipients: + Zac Hatfield-Dodds, levkivskyi
2019-11-18 00:53:54Zac Hatfield-Doddssetmessageid: <>
2019-11-18 00:53:54Zac Hatfield-Doddslinkissue38834 messages
2019-11-18 00:53:54Zac Hatfield-Doddscreate