Skip to content

[3.14] gh-133684: Fix get_annotations() where PEP 563 is involved (GH-133795) #134656

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 25, 2025
Merged
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
19 changes: 16 additions & 3 deletions Lib/annotationlib.py
Original file line number Diff line number Diff line change
@@ -1042,14 +1042,27 @@ def _get_and_call_annotate(obj, format):
return None


_BASE_GET_ANNOTATIONS = type.__dict__["__annotations__"].__get__


def _get_dunder_annotations(obj):
"""Return the annotations for an object, checking that it is a dictionary.

Does not return a fresh dictionary.
"""
ann = getattr(obj, "__annotations__", None)
if ann is None:
return None
# This special case is needed to support types defined under
# from __future__ import annotations, where accessing the __annotations__
# attribute directly might return annotations for the wrong class.
if isinstance(obj, type):
try:
ann = _BASE_GET_ANNOTATIONS(obj)
except AttributeError:
# For static types, the descriptor raises AttributeError.
return None
else:
ann = getattr(obj, "__annotations__", None)
if ann is None:
return None

if not isinstance(ann, dict):
raise ValueError(f"{obj!r}.__annotations__ is neither a dict nor None")
66 changes: 65 additions & 1 deletion Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
import functools
import itertools
import pickle
from string.templatelib import Interpolation, Template
from string.templatelib import Template
import typing
import unittest
from annotationlib import (
@@ -815,6 +815,70 @@ def test_stringized_annotations_on_class(self):
{"x": int},
)

def test_stringized_annotation_permutations(self):
def define_class(name, has_future, has_annos, base_text, extra_names=None):
lines = []
if has_future:
lines.append("from __future__ import annotations")
lines.append(f"class {name}({base_text}):")
if has_annos:
lines.append(f" {name}_attr: int")
else:
lines.append(" pass")
code = "\n".join(lines)
ns = support.run_code(code, extra_names=extra_names)
return ns[name]

def check_annotations(cls, has_future, has_annos):
if has_annos:
if has_future:
anno = "int"
else:
anno = int
self.assertEqual(get_annotations(cls), {f"{cls.__name__}_attr": anno})
else:
self.assertEqual(get_annotations(cls), {})

for meta_future, base_future, child_future, meta_has_annos, base_has_annos, child_has_annos in itertools.product(
(False, True),
(False, True),
(False, True),
(False, True),
(False, True),
(False, True),
):
with self.subTest(
meta_future=meta_future,
base_future=base_future,
child_future=child_future,
meta_has_annos=meta_has_annos,
base_has_annos=base_has_annos,
child_has_annos=child_has_annos,
):
meta = define_class(
"Meta",
has_future=meta_future,
has_annos=meta_has_annos,
base_text="type",
)
base = define_class(
"Base",
has_future=base_future,
has_annos=base_has_annos,
base_text="metaclass=Meta",
extra_names={"Meta": meta},
)
child = define_class(
"Child",
has_future=child_future,
has_annos=child_has_annos,
base_text="Base",
extra_names={"Base": base},
)
check_annotations(meta, meta_future, meta_has_annos)
check_annotations(base, base_future, base_has_annos)
check_annotations(child, child_future, child_has_annos)

def test_modify_annotations(self):
def f(x: int):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix bug where :func:`annotationlib.get_annotations` would return the wrong
result for certain classes that are part of a class hierarchy where ``from
__future__ import annotations`` is used.
Loading
Oops, something went wrong.

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant