mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-40396: Support GenericAlias in the typing functions. (GH-19718)
This commit is contained in:
parent
cfaf4c09ab
commit
68b352a698
3 changed files with 62 additions and 6 deletions
|
@ -22,7 +22,7 @@ from typing import NewType
|
|||
from typing import NamedTuple, TypedDict
|
||||
from typing import IO, TextIO, BinaryIO
|
||||
from typing import Pattern, Match
|
||||
from typing import Annotated
|
||||
from typing import Annotated, ForwardRef
|
||||
import abc
|
||||
import typing
|
||||
import weakref
|
||||
|
@ -1756,11 +1756,17 @@ class GenericTests(BaseTestCase):
|
|||
|
||||
def test_generic_forward_ref(self):
|
||||
def foobar(x: List[List['CC']]): ...
|
||||
def foobar2(x: list[list[ForwardRef('CC')]]): ...
|
||||
class CC: ...
|
||||
self.assertEqual(
|
||||
get_type_hints(foobar, globals(), locals()),
|
||||
{'x': List[List[CC]]}
|
||||
)
|
||||
self.assertEqual(
|
||||
get_type_hints(foobar2, globals(), locals()),
|
||||
{'x': list[list[CC]]}
|
||||
)
|
||||
|
||||
T = TypeVar('T')
|
||||
AT = Tuple[T, ...]
|
||||
def barfoo(x: AT): ...
|
||||
|
@ -2446,6 +2452,12 @@ class ForwardRefTests(BaseTestCase):
|
|||
self.assertEqual(get_type_hints(foo, globals(), locals()),
|
||||
{'a': Tuple[T]})
|
||||
|
||||
def foo(a: tuple[ForwardRef('T')]):
|
||||
pass
|
||||
|
||||
self.assertEqual(get_type_hints(foo, globals(), locals()),
|
||||
{'a': tuple[T]})
|
||||
|
||||
def test_forward_recursion_actually(self):
|
||||
def namespace1():
|
||||
a = typing.ForwardRef('A')
|
||||
|
@ -2909,6 +2921,18 @@ class GetTypeHintTests(BaseTestCase):
|
|||
get_type_hints(foobar, globals(), locals(), include_extras=True),
|
||||
{'x': List[Annotated[int, (1, 10)]]}
|
||||
)
|
||||
|
||||
def foobar(x: list[ForwardRef('X')]): ...
|
||||
X = Annotated[int, (1, 10)]
|
||||
self.assertEqual(
|
||||
get_type_hints(foobar, globals(), locals()),
|
||||
{'x': list[int]}
|
||||
)
|
||||
self.assertEqual(
|
||||
get_type_hints(foobar, globals(), locals(), include_extras=True),
|
||||
{'x': list[Annotated[int, (1, 10)]]}
|
||||
)
|
||||
|
||||
BA = Tuple[Annotated[T, (1, 0)], ...]
|
||||
def barfoo(x: BA): ...
|
||||
self.assertEqual(get_type_hints(barfoo, globals(), locals())['x'], Tuple[T, ...])
|
||||
|
@ -2916,12 +2940,22 @@ class GetTypeHintTests(BaseTestCase):
|
|||
get_type_hints(barfoo, globals(), locals(), include_extras=True)['x'],
|
||||
BA
|
||||
)
|
||||
|
||||
BA = tuple[Annotated[T, (1, 0)], ...]
|
||||
def barfoo(x: BA): ...
|
||||
self.assertEqual(get_type_hints(barfoo, globals(), locals())['x'], tuple[T, ...])
|
||||
self.assertIs(
|
||||
get_type_hints(barfoo, globals(), locals(), include_extras=True)['x'],
|
||||
BA
|
||||
)
|
||||
|
||||
def barfoo2(x: typing.Callable[..., Annotated[List[T], "const"]],
|
||||
y: typing.Union[int, Annotated[T, "mutable"]]): ...
|
||||
self.assertEqual(
|
||||
get_type_hints(barfoo2, globals(), locals()),
|
||||
{'x': typing.Callable[..., List[T]], 'y': typing.Union[int, T]}
|
||||
)
|
||||
|
||||
BA2 = typing.Callable[..., List[T]]
|
||||
def barfoo3(x: BA2): ...
|
||||
self.assertIs(
|
||||
|
@ -2972,6 +3006,9 @@ class GetUtilitiesTestCase(TestCase):
|
|||
self.assertIs(get_origin(Generic[T]), Generic)
|
||||
self.assertIs(get_origin(List[Tuple[T, T]][int]), list)
|
||||
self.assertIs(get_origin(Annotated[T, 'thing']), Annotated)
|
||||
self.assertIs(get_origin(List), list)
|
||||
self.assertIs(get_origin(list[int]), list)
|
||||
self.assertIs(get_origin(list), None)
|
||||
|
||||
def test_get_args(self):
|
||||
T = TypeVar('T')
|
||||
|
@ -2993,6 +3030,9 @@ class GetUtilitiesTestCase(TestCase):
|
|||
self.assertEqual(get_args(Tuple[int, ...]), (int, ...))
|
||||
self.assertEqual(get_args(Tuple[()]), ((),))
|
||||
self.assertEqual(get_args(Annotated[T, 'one', 2, ['three']]), (T, 'one', 2, ['three']))
|
||||
self.assertEqual(get_args(List), (typing.T,))
|
||||
self.assertEqual(get_args(list[int]), (int,))
|
||||
self.assertEqual(get_args(list), ())
|
||||
|
||||
|
||||
class CollectionsAbcTests(BaseTestCase):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue