Skip to content

Commit

Permalink
pythongh-109543: Remove unnecessary hasattr check (python#109544)
Browse files Browse the repository at this point in the history
Also added a new test case covering the scenario I thought this
might be about.
  • Loading branch information
JelleZijlstra authored and csm10495 committed Sep 28, 2023
1 parent b7fe6da commit cd91e0b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Lib/test/test_typing.py
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions Lib/typing.py
Expand Up @@ -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
Expand Down
@@ -0,0 +1,2 @@
Remove unnecessary :func:`hasattr` check during :data:`typing.TypedDict`
creation.

0 comments on commit cd91e0b

Please sign in to comment.