Skip to content

Commit

Permalink
pythongh-114053: Fix bad interaction of PEP 695, PEP 563 and `inspect…
Browse files Browse the repository at this point in the history
….get_annotations`
  • Loading branch information
AlexWaygood committed Jun 8, 2024
1 parent 38a25e9 commit ac5fd65
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
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: ...
33 changes: 33 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,38 @@ 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.keys(), {"x", "y", "z"})
self.assertEqual(
set(B_annotations.values()).intersection(ann_module695.B.__type_params__),
set()
)

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.

0 comments on commit ac5fd65

Please sign in to comment.