10000 [3.8] bpo-40398: Fix typing.get_args() for special generic aliases. (GH-19720) by serhiy-storchaka · Pull Request #19857 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.8] bpo-40398: Fix typing.get_args() for special generic aliases. (GH-19720) #19857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Lib/test/test_typing.py
< DD1E td id="diff-04d29c98076c2d6bb75921ea9becb26a862544d39b71db87b6e354c759b9305dR2944" data-line-number="2944" class="blob-num blob-num-context js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
Expand Up @@ -2913,6 +2913,9 @@ class C(Generic[T]): pass
self.assertIs(get_origin(Generic), Generic)
self.assertIs(get_origin(Generic[T]), Generic)
self.assertIs(get_origin(List[Tuple[T, T]][int]), list)
self.assertIs(get_origin(List), list)
self.assertIs(get_origin(Tuple), tuple)
self.assertIs(get_origin(Callable), collections.abc.Callable)

def test_get_args(self):
T = TypeVar('T')
Expand All @@ -2928,11 +2931,15 @@ class C(Generic[T]): pass
(int, Tuple[str, int]))
self.assertEqual(get_args(typing.Dict[int, Tuple[T, T]][Optional[int]]),
(int, Tuple[Optional[int], Optional[int]]))
self.assertEqual(get_args(Callable[[], T][int]), ([], int,))
self.assertEqual(get_args(Callable[[], T][int]), ([], int))
self.assertEqual(get_args(Callable[..., int]), (..., int))
self.assertEqual(get_args(Union[int, Callable[[Tuple[T, ...]], str]]),
(int, Callable[[Tuple[T, ...]], str]))
self.assertEqual(get_args(Tuple[int, ...]), (int, ...))
self.assertEqual(get_args(Tuple[()]), ((),))
self.assertEqual(get_args(List), ())
self.assertEqual(get_args(Tuple), ())
self.assertEqual(get_args(Callable), ())


class CollectionsAbcTests(BaseTestCase):
Expand Down
2 changes: 1 addition & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ def get_args(tp):
get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
get_args(Callable[[], T][int]) == ([], int)
"""
if isinstance(tp, _GenericAlias):
if isinstance(tp, _GenericAlias) and not tp._special:
res = tp.__args__
if get_origin(tp) is collections.abc.Callable and res[0] is not Ellipsis:
res = (list(res[:-1]), res[-1])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`typing.get_args` now always returns an empty tuple for special
generic aliases.
0