Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1293fcc

Browse filesBrowse files
gh-109543: Remove unnecessary hasattr check (#109544)
Also added a new test case covering the scenario I thought this might be about.
1 parent 81cd1bd commit 1293fcc
Copy full SHA for 1293fcc

File tree

3 files changed

+14
-2
lines changed
Filter options

3 files changed

+14
-2
lines changed

‎Lib/test/test_typing.py

Copy file name to clipboardExpand all lines: Lib/test/test_typing.py
+11Lines 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

Copy file name to clipboardExpand all lines: Lib/typing.py
+1-2Lines 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
+2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.