|
32 | 32 | from typing import ParamSpec, Concatenate, ParamSpecArgs, ParamSpecKwargs
|
33 | 33 | from typing import TypeGuard
|
34 | 34 | import abc
|
| 35 | +import textwrap |
35 | 36 | import typing
|
36 | 37 | import weakref
|
37 | 38 | import types
|
@@ -2156,6 +2157,45 @@ def barfoo(x: AT): ...
|
2156 | 2157 | def barfoo2(x: CT): ...
|
2157 | 2158 | self.assertIs(get_type_hints(barfoo2, globals(), locals())['x'], CT)
|
2158 | 2159 |
|
| 2160 | + def test_generic_pep585_forward_ref(self): |
| 2161 | + # See https://bugs.python.org/issue41370 |
| 2162 | + |
| 2163 | + class C1: |
| 2164 | + a: list['C1'] |
| 2165 | + self.assertEqual( |
| 2166 | + get_type_hints(C1, globals(), locals()), |
| 2167 | + {'a': list[C1]} |
| 2168 | + ) |
| 2169 | + |
| 2170 | + class C2: |
| 2171 | + a: dict['C1', list[List[list['C2']]]] |
| 2172 | + self.assertEqual( |
| 2173 | + get_type_hints(C2, globals(), locals()), |
| 2174 | + {'a': dict[C1, list[List[list[C2]]]]} |
| 2175 | + ) |
| 2176 | + |
| 2177 | + # Test stringified annotations |
| 2178 | + scope = {} |
| 2179 | + exec(textwrap.dedent(''' |
| 2180 | + from __future__ import annotations |
| 2181 | + class C3: |
| 2182 | + a: List[list["C2"]] |
| 2183 | + '''),
8000
scope) |
| 2184 | + C3 = scope['C3'] |
| 2185 | + self.assertEqual(C3.__annotations__['a'], "List[list['C2']]") |
| 2186 | + self.assertEqual( |
| 2187 | + get_type_hints(C3, globals(), locals()), |
| 2188 | + {'a': List[list[C2]]} |
| 2189 | + ) |
| 2190 | + |
| 2191 | + # Test recursive types |
| 2192 | + X = list["X"] |
| 2193 | + def f(x: X): ... |
| 2194 | + self.assertEqual( |
| 2195 | + get_type_hints(f, globals(), locals()), |
| 2196 | + {'x': list[list[ForwardRef('X')]]} |
| 2197 | + ) |
| 2198 | + |
2159 | 2199 | def test_extended_generic_rules_subclassing(self):
|
2160 | 2200 | class T1(Tuple[T, KT]): ...
|
2161 | 2201 | class T2(Tuple[T, ...]): ...
|
@@ -3556,15 +3596,15 @@ def foobar(x: list[ForwardRef('X')]): ...
|
3556 | 3596 | BA = Tuple[Annotated[T, (1, 0)], ...]
|
3557 | 3597 | def barfoo(x: BA): ...
|
3558 | 3598 | self.assertEqual(get_type_hints(barfoo, globals(), locals())['x'], Tuple[T, ...])
|
3559 |
| - self.assertIs( |
| 3599 | + self.assertEqual( |
3560 | 3600 | get_type_hints(barfoo, globals(), locals(), include_extras=True)['x'],
|
3561 | 3601 | BA
|
3562 | 3602 | )
|
3563 | 3603 |
|
3564 | 3604 | BA = tuple[Annotated[T, (1, 0)], ...]
|
3565 | 3605 | def barfoo(x: BA): ...
|
3566 | 3606 | self.assertEqual(get_type_hints(barfoo, globals(), locals())['x'], tuple[T, ...])
|
3567 |
| - self.assertIs( |
| 3607 | + self.assertEqual( |
3568 | 3608 | get_type_hints(barfoo, globals(), locals(), include_extras=True)['x'],
|
3569 | 3609 | BA
|
3570 | 3610 | )
|
|
0 commit comments