MCPcopy Index your code
hub / github.com/python/cpython / test_generic_pep585_forward_ref

Method test_generic_pep585_forward_ref

Lib/test/test_typing.py:5195–5232  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

5193 self.assertIs(get_type_hints(barfoo2, globals(), locals())['x'], CT)
5194
5195 def test_generic_pep585_forward_ref(self):
5196 # See https://bugs.python.org/issue41370
5197
5198 class C1:
5199 a: list['C1']
5200 self.assertEqual(
5201 get_type_hints(C1, globals(), locals()),
5202 {'a': list[C1]}
5203 )
5204
5205 class C2:
5206 a: dict['C1', list[List[list['C2']]]]
5207 self.assertEqual(
5208 get_type_hints(C2, globals(), locals()),
5209 {'a': dict[C1, list[List[list[C2]]]]}
5210 )
5211
5212 # Test stringified annotations
5213 scope = {}
5214 exec(textwrap.dedent('''
5215 from __future__ import annotations
5216 class C3:
5217 a: List[list["C2"]]
5218 '''), scope)
5219 C3 = scope['C3']
5220 self.assertEqual(C3.__annotations__['a'], "List[list['C2']]")
5221 self.assertEqual(
5222 get_type_hints(C3, globals(), locals()),
5223 {'a': List[list[C2]]}
5224 )
5225
5226 # Test recursive types
5227 X = list["X"]
5228 def f(x: X): ...
5229 self.assertEqual(
5230 get_type_hints(f, globals(), locals()),
5231 {'x': list[list[EqualToForwardRef('X')]]}
5232 )
5233
5234 def test_pep695_generic_class_with_future_annotations(self):
5235 original_globals = dict(ann_module695.__dict__)

Callers

nothing calls this directly

Calls 4

get_type_hintsFunction · 0.90
EqualToForwardRefClass · 0.90
assertEqualMethod · 0.45
dedentMethod · 0.45

Tested by

no test coverage detected