Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f03f4b1

Browse files
[3.12] Add some more edge-case tests for inspect.get_annotations with eval_str=True (GH-120550) (#120552)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 4fb2af3 commit f03f4b1

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

Lines changed: 19 additions & 4 deletions
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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,26 +1583,36 @@ def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_v
15831583
)
15841584
)
15851585

1586+
def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_and_local_vars(self):
1587+
self.assertEqual(
1588+
inspect.get_annotations(
1589+
inspect_stringized_annotations_pep695.E, eval_str=True
1590+
),
1591+
{"x": str},
1592+
)
1593+
15861594
def test_pep_695_generics_with_future_annotations_nested_in_function(self):
15871595
results = inspect_stringized_annotations_pep695.nested()
15881596

15891597
self.assertEqual(
1590-
set(results.E_annotations.values()),
1591-
set(results.E.__type_params__)
1598+
set(results.F_annotations.values()),
1599+
set(results.F.__type_params__)
15921600
)
15931601
self.assertEqual(
1594-
set(results.E_meth_annotations.values()),
1595-
set(results.E.generic_method.__type_params__)
1602+
set(results.F_meth_annotations.values()),
1603+
set(results.F.generic_method.__type_params__)
15961604
)
15971605
self.assertNotEqual(
1598-
set(results.E_meth_annotations.values()),
1599-
set(results.E.__type_params__)
1606+
set(results.F_meth_annotations.values()),
1607+
set(results.F.__type_params__)
16001608
)
16011609
self.assertEqual(
1602-
set(results.E_meth_annotations.values()).intersection(results.E.__type_params__),
1610+
set(results.F_meth_annotations.values()).intersection(results.F.__type_params__),
16031611
set()
16041612
)
16051613

1614+
self.assertEqual(results.G_annotations, {"x": str})
1615+
16061616
self.assertEqual(
16071617
set(results.generic_func_annotations.values()),
16081618
set(results.generic_func.__type_params__)

0 commit comments

Comments
 (0)