gh-109543: Remove unnecessary hasattr check (#109544)

Also added a new test case covering the scenario I thought this
might be about.
This commit is contained in:
Jelle Zijlstra 2023-09-19 20:15:52 -07:00 committed by GitHub
parent 81cd1bd713
commit 1293fcc3c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 2 deletions

View File

@ -7586,6 +7586,17 @@ class TypedDictTests(BaseTestCase):
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

View File

@ -2886,8 +2886,7 @@ class _TypedDictMeta(type):
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

View File

@ -0,0 +1,2 @@
Remove unnecessary :func:`hasattr` check during :data:`typing.TypedDict`
creation.