Better runtime TypedDict (GH-17214)

This patch enables downstream projects inspecting a TypedDict subclass at runtime to tell which keys are optional.

This is essential for generating test data with Hypothesis or validating inputs with typeguard or pydantic.
This commit is contained in:
Zac Hatfield-Dodds 2019-11-24 21:48:48 +11:00 committed by Ivan Levkivskyi
parent 041d8b48a2
commit 665ad3dfa9
3 changed files with 25 additions and 3 deletions

View file

@ -3741,6 +3741,13 @@ def test_total(self):
self.assertEqual(Options(log_level=2), {'log_level': 2})
self.assertEqual(Options.__total__, False)
def test_optional_keys(self):
class Point2Dor3D(Point2D, total=False):
z: int
assert Point2Dor3D.__required_keys__ == frozenset(['x', 'y'])
assert Point2Dor3D.__optional_keys__ == frozenset(['z'])
class IOTests(BaseTestCase):