mirror of
https://github.com/python/cpython.git
synced 2025-11-02 19:12:55 +00:00
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:
parent
041d8b48a2
commit
665ad3dfa9
3 changed files with 25 additions and 3 deletions
|
|
@ -3741,6 +3741,13 @@ class TypedDictTests(BaseTestCase):
|
|||
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):
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue