summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorAlex Waygood <Alex.Waygood@Gmail.com>2024-04-19 13:41:28 (GMT)
committerGitHub <noreply@github.com>2024-04-19 13:41:28 (GMT)
commit5430f614371530aab1178b3b610add4bf54c383e (patch)
treeb06c6fbeac3bc747ea00b4715b90e15025db47d6 /Lib/test
parent6584ad9aa7b2940d5e8291453c676371304d6dc4 (diff)
downloadcpython-5430f614371530aab1178b3b610add4bf54c383e.zip
cpython-5430f614371530aab1178b3b610add4bf54c383e.tar.gz
cpython-5430f614371530aab1178b3b610add4bf54c383e.tar.bz2
[3.12] gh-114053: Fix bad interaction of PEP-695, PEP-563 and ``get_type_hints`` (#118009) (#118104)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_typing.py26
-rw-r--r--Lib/test/typinganndata/ann_module695.py22
2 files changed, 47 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index dc117b3..e7cb01a 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -47,7 +47,7 @@ import weakref
import types
from test.support import captured_stderr, cpython_only
-from test.typinganndata import mod_generics_cache, _typed_dict_helper
+from test.typinganndata import ann_module695, mod_generics_cache, _typed_dict_helper
CANNOT_SUBCLASS_TYPE = 'Cannot subclass special typing classes'
@@ -4499,6 +4499,30 @@ class GenericTests(BaseTestCase):
{'x': list[list[ForwardRef('X')]]}
)
+ def test_pep695_generic_with_future_annotations(self):
+ hints_for_A = get_type_hints(ann_module695.A)
+ A_type_params = ann_module695.A.__type_params__
+ self.assertIs(hints_for_A["x"], A_type_params[0])
+ self.assertEqual(hints_for_A["y"].__args__[0], Unpack[A_type_params[1]])
+ self.assertIs(hints_for_A["z"].__args__[0], A_type_params[2])
+
+ hints_for_B = get_type_hints(ann_module695.B)
+ self.assertEqual(hints_for_B.keys(), {"x", "y", "z"})
+ self.assertEqual(
+ set(hints_for_B.values()) ^ set(ann_module695.B.__type_params__),
+ set()
+ )
+
+ hints_for_generic_function = get_type_hints(ann_module695.generic_function)
+ func_t_params = ann_module695.generic_function.__type_params__
+ self.assertEqual(
+ hints_for_generic_function.keys(), {"x", "y", "z", "zz", "return"}
+ )
+ self.assertIs(hints_for_generic_function["x"], func_t_params[0])
+ self.assertEqual(hints_for_generic_function["y"], Unpack[func_t_params[1]])
+ self.assertIs(hints_for_generic_function["z"].__origin__, func_t_params[2])
+ self.assertIs(hints_for_generic_function["zz"].__origin__, func_t_params[2])
+
def test_extended_generic_rules_subclassing(self):
class T1(Tuple[T, KT]): ...
class T2(Tuple[T, ...]): ...
diff --git a/Lib/test/typinganndata/ann_module695.py b/Lib/test/typinganndata/ann_module695.py
new file mode 100644
index 0000000..2ede9fe
--- /dev/null
+++ b/Lib/test/typinganndata/ann_module695.py
@@ -0,0 +1,22 @@
+from __future__ import annotations
+from typing import Callable
+
+
+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: *Ts, z: P.args, zz: P.kwargs
+) -> None: ...