Skip to content

Commit b54511b

Browse files
JelleZijlstraGlyphack
authored andcommitted
pythongh-109543: Remove unnecessary hasattr check (python#109544)
Also added a new test case covering the scenario I thought this might be about.
1 parent 8d3c7a6 commit b54511b

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Lib/test/test_typing.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7586,6 +7586,17 @@ def test_total(self):
75867586
self.assertEqual(Options.__required_keys__, frozenset())
75877587
self.assertEqual(Options.__optional_keys__, {'log_level', 'log_path'})
75887588

7589+
def test_total_inherits_non_total(self):
7590+
class TD1(TypedDict, total=False):
7591+
a: int
7592+
7593+
self.assertIs(TD1.__total__, False)
7594+
7595+
class TD2(TD1):
7596+
b: str
7597+
7598+
self.assertIs(TD2.__total__, True)
7599+
75897600
def test_optional_keys(self):
75907601
class Point2Dor3D(Point2D, total=False):
75917602
z: int

Lib/typing.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,8 +2886,7 @@ def __new__(cls, name, bases, ns, total=True):
28862886
tp_dict.__annotations__ = annotations
28872887
tp_dict.__required_keys__ = frozenset(required_keys)
28882888
tp_dict.__optional_keys__ = frozenset(optional_keys)
2889-
if not hasattr(tp_dict, '__total__'):
2890-
tp_dict.__total__ = total
2889+
tp_dict.__total__ = total
28912890
return tp_dict
28922891

28932892
__call__ = dict # static method
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove unnecessary :func:`hasattr` check during :data:`typing.TypedDict`
2+
creation.

0 commit comments

Comments
 (0)