Skip to content

gh-114053: Fix bad interaction of PEP 695, PEP 563 and inspect.get_annotations #120270

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 6 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,13 @@ def get_annotations(obj, *, globals=None, locals=None, eval_str=False):
if globals is None:
globals = obj_globals
if locals is None:
locals = obj_locals
locals = obj_locals or {}

# "Inject" type parameters into the local namespace
# (unless they are shadowed by assignments *in* the local namespace),
# as a way of emulating annotation scopes when calling `eval()`
if type_params := getattr(obj, "__type_params__", ()):
locals = {param.__name__: param for param in type_params} | locals

return_value = {key:
value if not isinstance(value, str) else eval(value, globals, locals)
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_inspect/inspect_stringized_annotations_pep695.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

from __future__ import annotations
from typing import Callable, Unpack


class A[T, *Ts, **P]:
x: T
y: tuple[*Ts]
z: Callable[P, str]


class B[T, *Ts, **P]:
T = int
Ts = str
P = bytes
x: T
y: Ts
z: P


def generic_function[T, *Ts, **P](
x: T, *y: Unpack[Ts], z: P.args, zz: P.kwargs
) -> None: ...
29 changes: 29 additions & 0 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from test.test_inspect import inspect_stock_annotations
from test.test_inspect import inspect_stringized_annotations
from test.test_inspect import inspect_stringized_annotations_2
from test.test_inspect import inspect_stringized_annotations_pep695


# Functions tested in this suite:
Expand Down Expand Up @@ -1692,6 +1693,34 @@ def wrapper(a, b):
self.assertEqual(inspect.get_annotations(isa.MyClassWithLocalAnnotations), {'x': 'mytype'})
self.assertEqual(inspect.get_annotations(isa.MyClassWithLocalAnnotations, eval_str=True), {'x': int})

def test_get_annotations_with_stringized_pep695_annotations(self):
from typing import Unpack
ann_module695 = inspect_stringized_annotations_pep695

A_annotations = inspect.get_annotations(ann_module695.A, eval_str=True)
A_type_params = ann_module695.A.__type_params__
self.assertIs(A_annotations["x"], A_type_params[0])
self.assertEqual(A_annotations["y"].__args__[0], Unpack[A_type_params[1]])
self.assertIs(A_annotations["z"].__args__[0], A_type_params[2])

B_annotations = inspect.get_annotations(ann_module695.B, eval_str=True)
self.assertEqual(B_annotations, {"x": int, "y": str, "z": bytes})

generic_function_annotations = inspect.get_annotations(
ann_module695.generic_function, eval_str=True
)
func_t_params = ann_module695.generic_function.__type_params__
self.assertEqual(
generic_function_annotations.keys(), {"x", "y", "z", "zz", "return"}
)
self.assertIs(generic_function_annotations["x"], func_t_params[0])
self.assertEqual(
generic_function_annotations["y"],
Unpack[func_t_params[1]]
)
self.assertIs(generic_function_annotations["z"].__origin__, func_t_params[2])
self.assertIs(generic_function_annotations["zz"].__origin__, func_t_params[2])


class TestFormatAnnotation(unittest.TestCase):
def test_typing_replacement(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix erroneous :exc:`NameError` when calling :func:`inspect.get_annotations`
with ``eval_str=True``` on a class that made use of :pep:`695` type
parameters in a module that had ``from __future__ import annotations`` at
the top of the file. Patch by Alex Waygood.
Loading