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

gh-133701: Fix incorrect __annotations__ on TypedDict defined under PEP 563 #133772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2025
Merged
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
4 changes: 3 additions & 1 deletion 4 Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,11 @@ def sortdict(dict):
return "{%s}" % withcommas


def run_code(code: str) -> dict[str, object]:
def run_code(code: str, extra_names: dict[str, object] | None = None) -> dict[str, object]:
"""Run a piece of code after dedenting it, and return its global namespace."""
ns = {}
if extra_names:
ns.update(extra_names)
exec(textwrap.dedent(code), ns)
return ns

Expand Down
30 changes: 30 additions & 0 deletions 30 Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8538,6 +8538,36 @@ class Child(Base1, Base2):
self.assertEqual(Child.__required_keys__, frozenset(['a']))
self.assertEqual(Child.__optional_keys__, frozenset())

def test_inheritance_pep563(self):
def _make_td(future, class_name, annos, base, extra_names=None):
lines = []
if future:
lines.append('from __future__ import annotations')
lines.append('from typing import TypedDict')
lines.append(f'class {class_name}({base}):')
for name, anno in annos.items():
lines.append(f' {name}: {anno}')
code = '\n'.join(lines)
ns = run_code(code, extra_names)
return ns[class_name]

for base_future in (True, False):
for child_future in (True, False):
with self.subTest(base_future=base_future, child_future=child_future):
base = _make_td(
base_future, "Base", {"base": "int"}, "TypedDict"
)
self.assertIsNotNone(base.__annotate__)
child = _make_td(
child_future, "Child", {"child": "int"}, "Base", {"Base": base}
)
base_anno = ForwardRef("int", module="builtins") if base_future else int
child_anno = ForwardRef("int", module="builtins") if child_future else int
self.assertEqual(base.__annotations__, {'base': base_anno})
self.assertEqual(
child.__annotations__, {'child': child_anno, 'base': base_anno}
)

def test_required_notrequired_keys(self):
self.assertEqual(NontotalMovie.__required_keys__,
frozenset({"title"}))
Expand Down
8 changes: 5 additions & 3 deletions 8 Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3084,14 +3084,16 @@ def __new__(cls, name, bases, ns, total=True):
else:
generic_base = ()

ns_annotations = ns.pop('__annotations__', None)

tp_dict = type.__new__(_TypedDictMeta, name, (*generic_base, dict), ns)

if not hasattr(tp_dict, '__orig_bases__'):
tp_dict.__orig_bases__ = bases

if "__annotations__" in ns:
if ns_annotations is not None:
own_annotate = None
own_annotations = ns["__annotations__"]
own_annotations = ns_annotations
elif (own_annotate := _lazy_annotationlib.get_annotate_from_class_namespace(ns)) is not None:
own_annotations = _lazy_annotationlib.call_annotate_function(
own_annotate, _lazy_annotationlib.Format.FORWARDREF, owner=tp_dict
Expand Down Expand Up @@ -3162,7 +3164,7 @@ def __annotate__(format):
if base_annotate is None:
continue
base_annos = _lazy_annotationlib.call_annotate_function(
base.__annotate__, format, owner=base)
base_annotate, format, owner=base)
annos.update(base_annos)
if own_annotate is not None:
own = _lazy_annotationlib.call_annotate_function(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix bug where :class:`typing.TypedDict` classes defined under ``from
__future__ import annotations`` and inheriting from another ``TypedDict``
had an incorrect ``__annotations__`` attribute.
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.