Skip to content

Commit 99d62f9

Browse files
authored
Add some more edge-case tests for inspect.get_annotations with eval_str=True (#120550)
1 parent c501261 commit 99d62f9

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

Lib/test/test_inspect/inspect_stringized_annotations_pep695.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ def generic_method[Foo, **Bar](
4545
def generic_method_2[Eggs, **Spam](self, x: Eggs, y: Spam): pass
4646

4747

48+
# Eggs is `int` in globals, a TypeVar in type_params, and `str` in locals:
49+
class E[Eggs]:
50+
Eggs = str
51+
x: Eggs
52+
53+
54+
4855
def nested():
4956
from types import SimpleNamespace
5057
from inspect import get_annotations
@@ -53,7 +60,7 @@ def nested():
5360
Spam = memoryview
5461

5562

56-
class E[Eggs, **Spam]:
63+
class F[Eggs, **Spam]:
5764
x: Eggs
5865
y: Spam
5966

@@ -63,10 +70,18 @@ def generic_method[Eggs, **Spam](self, x: Eggs, y: Spam): pass
6370
def generic_function[Eggs, **Spam](x: Eggs, y: Spam): pass
6471

6572

73+
# Eggs is `int` in globals, `bytes` in the function scope,
74+
# a TypeVar in the type_params, and `str` in locals:
75+
class G[Eggs]:
76+
Eggs = str
77+
x: Eggs
78+
79+
6680
return SimpleNamespace(
67-
E=E,
68-
E_annotations=get_annotations(E, eval_str=True),
69-
E_meth_annotations=get_annotations(E.generic_method, eval_str=True),
81+
F=F,
82+
F_annotations=get_annotations(F, eval_str=True),
83+
F_meth_annotations=get_annotations(F.generic_method, eval_str=True),
84+
G_annotations=get_annotations(G, eval_str=True),
7085
generic_func=generic_function,
7186
generic_func_annotations=get_annotations(generic_function, eval_str=True)
7287
)

Lib/test/test_inspect/test_inspect.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -1770,26 +1770,36 @@ def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_v
17701770
)
17711771
)
17721772

1773+
def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_and_local_vars(self):
1774+
self.assertEqual(
1775+
inspect.get_annotations(
1776+
inspect_stringized_annotations_pep695.E, eval_str=True
1777+
),
1778+
{"x": str},
1779+
)
1780+
17731781
def test_pep_695_generics_with_future_annotations_nested_in_function(self):
17741782
results = inspect_stringized_annotations_pep695.nested()
17751783

17761784
self.assertEqual(
1777-
set(results.E_annotations.values()),
1778-
set(results.E.__type_params__)
1785+
set(results.F_annotations.values()),
1786+
set(results.F.__type_params__)
17791787
)
17801788
self.assertEqual(
1781-
set(results.E_meth_annotations.values()),
1782-
set(results.E.generic_method.__type_params__)
1789+
set(results.F_meth_annotations.values()),
1790+
set(results.F.generic_method.__type_params__)
17831791
)
17841792
self.assertNotEqual(
1785-
set(results.E_meth_annotations.values()),
1786-
set(results.E.__type_params__)
1793+
set(results.F_meth_annotations.values()),
1794+
set(results.F.__type_params__)
17871795
)
17881796
self.assertEqual(
1789-
set(results.E_meth_annotations.values()).intersection(results.E.__type_params__),
1797+
set(results.F_meth_annotations.values()).intersection(results.F.__type_params__),
17901798
set()
17911799
)
17921800

1801+
self.assertEqual(results.G_annotations, {"x": str})
1802+
17931803
self.assertEqual(
17941804
set(results.generic_func_annotations.values()),
17951805
set(results.generic_func.__type_params__)

0 commit comments

Comments
 (0)