From cd91e0b9a55f29845b7e66e0c9d00b945988d540 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 19 Sep 2023 20:15:52 -0700 Subject: [PATCH] gh-109543: Remove unnecessary hasattr check (#109544) Also added a new test case covering the scenario I thought this might be about. --- Lib/test/test_typing.py | 11 +++++++++++ Lib/typing.py | 3 +-- .../2023-09-18-07-43-22.gh-issue-109543.1tOGoV.rst | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-09-18-07-43-22.gh-issue-109543.1tOGoV.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 69f5ff913c57bb..4d1c0f2c724b86 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -7586,6 +7586,17 @@ def test_total(self): self.assertEqual(Options.__required_keys__, frozenset()) self.assertEqual(Options.__optional_keys__, {'log_level', 'log_path'}) + def test_total_inherits_non_total(self): + class TD1(TypedDict, total=False): + a: int + + self.assertIs(TD1.__total__, False) + + class TD2(TD1): + b: str + + self.assertIs(TD2.__total__, True) + def test_optional_keys(self): class Point2Dor3D(Point2D, total=False): z: int diff --git a/Lib/typing.py b/Lib/typing.py index 8655b756a9fd13..183d5b29a23362 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -2886,8 +2886,7 @@ def __new__(cls, name, bases, ns, total=True): tp_dict.__annotations__ = annotations tp_dict.__required_keys__ = frozenset(required_keys) tp_dict.__optional_keys__ = frozenset(optional_keys) - if not hasattr(tp_dict, '__total__'): - tp_dict.__total__ = total + tp_dict.__total__ = total return tp_dict __call__ = dict # static method diff --git a/Misc/NEWS.d/next/Library/2023-09-18-07-43-22.gh-issue-109543.1tOGoV.rst b/Misc/NEWS.d/next/Library/2023-09-18-07-43-22.gh-issue-109543.1tOGoV.rst new file mode 100644 index 00000000000000..e790f7750c332a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-09-18-07-43-22.gh-issue-109543.1tOGoV.rst @@ -0,0 +1,2 @@ +Remove unnecessary :func:`hasattr` check during :data:`typing.TypedDict` +creation.