From 15e70c1d9fb0e5b4c2672837cdad8b8ccda67fc0 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 24 May 2023 11:01:23 -0700 Subject: [PATCH 1/2] Improve test coverage for TypedDict In particular, it's important to test that is_typeddict(TypedDict) returns False. --- Lib/test/test_typing.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 098933b7cb434f..d42faeb6004f46 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -7223,10 +7223,29 @@ class Wrong(*bases): pass def test_is_typeddict(self): - assert is_typeddict(Point2D) is True - assert is_typeddict(Union[str, int]) is False + self.assertTrue(is_typeddict(Point2D)) + self.assertFalse(is_typeddict(Union[str, int])) # classes, not instances - assert is_typeddict(Point2D()) is False + self.assertFalse(is_typeddict(Point2D())) + call_based = TypedDict('call_based', {'a': int}) + self.assertTrue(is_typeddict(call_based)) + self.assertFalse(is_typeddict(call_based())) + + T = TypeVar("T") + class BarGeneric(TypedDict, Generic[T]): + a: T + self.assertTrue(is_typeddict(BarGeneric)) + self.assertFalse(is_typeddict(BarGeneric[int])) + self.assertFalse(is_typeddict(BarGeneric())) + + class NewGeneric[T](TypedDict): + a: T + self.assertTrue(is_typeddict(NewGeneric)) + self.assertFalse(is_typeddict(NewGeneric[int])) + self.assertFalse(is_typeddict(NewGeneric())) + + # The TypedDict constructor is not itself a TypedDict + self.assertFalse(is_typeddict(TypedDict)) def test_get_type_hints(self): self.assertEqual( From 8dcecc4fb7ac7a55ffecae28a614e5b9e33b7650 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 24 May 2023 11:15:23 -0700 Subject: [PATCH 2/2] assertIs --- Lib/test/test_typing.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index d42faeb6004f46..76c9e39f1d70e6 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -7223,29 +7223,29 @@ class Wrong(*bases): pass def test_is_typeddict(self): - self.assertTrue(is_typeddict(Point2D)) - self.assertFalse(is_typeddict(Union[str, int])) + self.assertIs(is_typeddict(Point2D), True) + self.assertIs(is_typeddict(Union[str, int]), False) # classes, not instances - self.assertFalse(is_typeddict(Point2D())) + self.assertIs(is_typeddict(Point2D()), False) call_based = TypedDict('call_based', {'a': int}) - self.assertTrue(is_typeddict(call_based)) - self.assertFalse(is_typeddict(call_based())) + self.assertIs(is_typeddict(call_based), True) + self.assertIs(is_typeddict(call_based()), False) T = TypeVar("T") class BarGeneric(TypedDict, Generic[T]): a: T - self.assertTrue(is_typeddict(BarGeneric)) - self.assertFalse(is_typeddict(BarGeneric[int])) - self.assertFalse(is_typeddict(BarGeneric())) + self.assertIs(is_typeddict(BarGeneric), True) + self.assertIs(is_typeddict(BarGeneric[int]), False) + self.assertIs(is_typeddict(BarGeneric()), False) class NewGeneric[T](TypedDict): a: T - self.assertTrue(is_typeddict(NewGeneric)) - self.assertFalse(is_typeddict(NewGeneric[int])) - self.assertFalse(is_typeddict(NewGeneric())) + self.assertIs(is_typeddict(NewGeneric), True) + self.assertIs(is_typeddict(NewGeneric[int]), False) + self.assertIs(is_typeddict(NewGeneric()), False) # The TypedDict constructor is not itself a TypedDict - self.assertFalse(is_typeddict(TypedDict)) + self.assertIs(is_typeddict(TypedDict), False) def test_get_type_hints(self): self.assertEqual(