Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Fix setting of `__required_keys__` and `__optional_keys__` when inheriting
keys with the same name.
- Fix incorrect behaviour on Python 3.9 and Python 3.10 that meant that
calling `isinstance` with `typing_extensions.Concatenate[...]` or
`typing_extensions.Unpack[...]` as the first argument could have a different
Expand Down
41 changes: 41 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4638,6 +4638,47 @@ class ChildWithInlineAndOptional(Untotal, Inline):
class Wrong(*bases):
pass

def test_keys_inheritance_with_same_name(self):
class NotTotal(TypedDict, total=False):
a: int

class Total(NotTotal):
a: int

self.assertEqual(NotTotal.__required_keys__, frozenset())
self.assertEqual(NotTotal.__optional_keys__, frozenset(['a']))
self.assertEqual(Total.__required_keys__, frozenset(['a']))
self.assertEqual(Total.__optional_keys__, frozenset())

class Base(TypedDict):
a: NotRequired[int]
b: Required[int]

class Child(Base):
a: Required[int]
b: NotRequired[int]

self.assertEqual(Base.__required_keys__, frozenset(['b']))
self.assertEqual(Base.__optional_keys__, frozenset(['a']))
self.assertEqual(Child.__required_keys__, frozenset(['a']))
self.assertEqual(Child.__optional_keys__, frozenset(['b']))

def test_multiple_inheritance_with_same_key(self):
class Base1(TypedDict):
a: NotRequired[int]

class Base2(TypedDict):
a: Required[str]

class Child(Base1, Base2):
pass

# Last base wins
self.assertEqual(Child.__annotations__, {'a': Required[str]})
self.assertEqual(Child.__required_keys__, frozenset(['a']))
self.assertEqual(Child.__optional_keys__, frozenset())


def test_closed_values(self):
class Implicit(TypedDict): ...
class ExplicitTrue(TypedDict, closed=True): ...
Expand Down
22 changes: 17 additions & 5 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,8 +1182,14 @@ def __new__(cls, name, bases, ns, *, total=True, closed=None,

if sys.version_info <= (3, 14):
annotations.update(base_dict.get('__annotations__', {}))
required_keys.update(base_dict.get('__required_keys__', ()))
optional_keys.update(base_dict.get('__optional_keys__', ()))
base_required = base_dict.get('__required_keys__', set())
required_keys |= base_required
optional_keys -= base_required

base_optional = base_dict.get('__optional_keys__', set())
required_keys -= base_optional
optional_keys |= base_optional

readonly_keys.update(base_dict.get('__readonly_keys__', ()))
mutable_keys.update(base_dict.get('__mutable_keys__', ()))

Expand Down Expand Up @@ -1211,13 +1217,19 @@ def __new__(cls, name, bases, ns, *, total=True, closed=None,
qualifiers = set(_get_typeddict_qualifiers(annotation_type))

if Required in qualifiers:
required_keys.add(annotation_key)
is_required = True
elif NotRequired in qualifiers:
optional_keys.add(annotation_key)
elif total:
is_required = False
else:
is_required = total

if is_required:
required_keys.add(annotation_key)
optional_keys.discard(annotation_key)
else:
optional_keys.add(annotation_key)
required_keys.discard(annotation_key)

if ReadOnly in qualifiers:
mutable_keys.discard(annotation_key)
readonly_keys.add(annotation_key)
Expand Down
Loading