mirror of https://github.com/python/cpython
[3.13] Add some more edge-case tests for `inspect.get_annotations` with `eval_str=True` (GH-120550) (#120551)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
767c955544
commit
13a5082afe
|
@ -45,6 +45,13 @@ class D:
|
||||||
def generic_method_2[Eggs, **Spam](self, x: Eggs, y: Spam): pass
|
def generic_method_2[Eggs, **Spam](self, x: Eggs, y: Spam): pass
|
||||||
|
|
||||||
|
|
||||||
|
# Eggs is `int` in globals, a TypeVar in type_params, and `str` in locals:
|
||||||
|
class E[Eggs]:
|
||||||
|
Eggs = str
|
||||||
|
x: Eggs
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def nested():
|
def nested():
|
||||||
from types import SimpleNamespace
|
from types import SimpleNamespace
|
||||||
from inspect import get_annotations
|
from inspect import get_annotations
|
||||||
|
@ -53,7 +60,7 @@ def nested():
|
||||||
Spam = memoryview
|
Spam = memoryview
|
||||||
|
|
||||||
|
|
||||||
class E[Eggs, **Spam]:
|
class F[Eggs, **Spam]:
|
||||||
x: Eggs
|
x: Eggs
|
||||||
y: Spam
|
y: Spam
|
||||||
|
|
||||||
|
@ -63,10 +70,18 @@ def nested():
|
||||||
def generic_function[Eggs, **Spam](x: Eggs, y: Spam): pass
|
def generic_function[Eggs, **Spam](x: Eggs, y: Spam): pass
|
||||||
|
|
||||||
|
|
||||||
|
# Eggs is `int` in globals, `bytes` in the function scope,
|
||||||
|
# a TypeVar in the type_params, and `str` in locals:
|
||||||
|
class G[Eggs]:
|
||||||
|
Eggs = str
|
||||||
|
x: Eggs
|
||||||
|
|
||||||
|
|
||||||
return SimpleNamespace(
|
return SimpleNamespace(
|
||||||
E=E,
|
F=F,
|
||||||
E_annotations=get_annotations(E, eval_str=True),
|
F_annotations=get_annotations(F, eval_str=True),
|
||||||
E_meth_annotations=get_annotations(E.generic_method, eval_str=True),
|
F_meth_annotations=get_annotations(F.generic_method, eval_str=True),
|
||||||
|
G_annotations=get_annotations(G, eval_str=True),
|
||||||
generic_func=generic_function,
|
generic_func=generic_function,
|
||||||
generic_func_annotations=get_annotations(generic_function, eval_str=True)
|
generic_func_annotations=get_annotations(generic_function, eval_str=True)
|
||||||
)
|
)
|
||||||
|
|
|
@ -1770,26 +1770,36 @@ class TestClassesAndFunctions(unittest.TestCase):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_and_local_vars(self):
|
||||||
|
self.assertEqual(
|
||||||
|
inspect.get_annotations(
|
||||||
|
inspect_stringized_annotations_pep695.E, eval_str=True
|
||||||
|
),
|
||||||
|
{"x": str},
|
||||||
|
)
|
||||||
|
|
||||||
def test_pep_695_generics_with_future_annotations_nested_in_function(self):
|
def test_pep_695_generics_with_future_annotations_nested_in_function(self):
|
||||||
results = inspect_stringized_annotations_pep695.nested()
|
results = inspect_stringized_annotations_pep695.nested()
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
set(results.E_annotations.values()),
|
set(results.F_annotations.values()),
|
||||||
set(results.E.__type_params__)
|
set(results.F.__type_params__)
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
set(results.E_meth_annotations.values()),
|
set(results.F_meth_annotations.values()),
|
||||||
set(results.E.generic_method.__type_params__)
|
set(results.F.generic_method.__type_params__)
|
||||||
)
|
)
|
||||||
self.assertNotEqual(
|
self.assertNotEqual(
|
||||||
set(results.E_meth_annotations.values()),
|
set(results.F_meth_annotations.values()),
|
||||||
set(results.E.__type_params__)
|
set(results.F.__type_params__)
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
set(results.E_meth_annotations.values()).intersection(results.E.__type_params__),
|
set(results.F_meth_annotations.values()).intersection(results.F.__type_params__),
|
||||||
set()
|
set()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.assertEqual(results.G_annotations, {"x": str})
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
set(results.generic_func_annotations.values()),
|
set(results.generic_func_annotations.values()),
|
||||||
set(results.generic_func.__type_params__)
|
set(results.generic_func.__type_params__)
|
||||||
|
|
Loading…
Reference in New Issue