Skip to content
Merged
Prev Previous commit
Next Next commit
add unit test for from __future__ import annotations
  • Loading branch information
NiklasRosenstein committed Jan 27, 2022
commit 26ab74013e8274457724eba1a9e83a98a719176d
31 changes: 23 additions & 8 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from typing import ParamSpec, Concatenate, ParamSpecArgs, ParamSpecKwargs
from typing import TypeGuard
import abc
import textwrap
import typing
import weakref
import types
Expand Down Expand Up @@ -2009,18 +2010,32 @@ def barfoo2(x: CT): ...
def test_generic_pep585_forward_ref(self):
# See https://bugs.python.org/issue41370

class N:
a: list['N']
class C1:
a: list['C1']
self.assertEqual(
get_type_hints(C1, globals(), locals()),
{'a': list[C1]}
)

class C2:
a: dict['C1', list[List[list['C2']]]]
self.assertEqual(
get_type_hints(N, globals(), locals()),
{'a': list[N]}
get_type_hints(C2, globals(), locals()),
{'a': dict[C1, list[List[list[C2]]]]}
)

class M:
a: dict['N', list[List[list['M']]]]
scope = {}
exec(textwrap.dedent('''
from __future__ import annotations
class C3:
a: List[list["C2"]]
'''), scope)
C3 = scope['C3']

self.assertEqual(C3.__annotations__['a'], "List[list['C2']]")
self.assertEqual(
get_type_hints(M, globals(), locals()),
{'a': dict[N, list[List[list[M]]]]}
get_type_hints(C3, globals(), locals()),
{'a': List[list[C2]]}
)

def test_extended_generic_rules_subclassing(self):
Expand Down