gh-114053: Fix bad interaction of PEP-695, PEP-563 and `get_type_hints` (#118009)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Alex Waygood 2024-04-19 14:03:44 +01:00 committed by GitHub
parent 15b3555e4a
commit 1e3e7ce11e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 81 additions and 10 deletions

View file

@ -46,7 +46,7 @@ import weakref
import types
from test.support import captured_stderr, cpython_only, infinite_recursion
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'
@ -4641,6 +4641,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, ...]): ...