Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'master' into refactor-typing-generic-alias
  • Loading branch information
serhiy-storchaka committed Apr 26, 2020
commit e626caf9f3274e659945b15630aa41494478be2b
4 changes: 4 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,8 @@ class C(Generic[T]): pass
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')
Expand All @@ -3029,6 +3031,8 @@ class C(Generic[T]): pass
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):
Expand Down
16 changes: 13 additions & 3 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def _subs_tvars(tp, tvars, subs):
"""Substitute type variables 'tvars' with substitutions 'subs'.
These two must have the same length.
"""
if not isinstance(tp, _BaseGenericAlias):
if not isinstance(tp, (_BaseGenericAlias, GenericAlias)):
return tp
new_args = list(tp.__args__)
for a, arg in enumerate(tp.__args__):
Expand Down Expand Up @@ -278,6 +278,11 @@ def _eval_type(t, globalns, localns):
if ev_args == t.__args__:
return t
return t.copy_with(ev_args)
if isinstance(t, GenericAlias):
ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
if ev_args == t.__args__:
return t
return GenericAlias(t.__origin__, ev_args)
return t


Expand Down Expand Up @@ -1372,6 +1377,11 @@ def _strip_annotations(t):
if stripped_args == t.__args__:
return t
return t.copy_with(stripped_args)
if isinstance(t, GenericAlias):
stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
if stripped_args == t.__args__:
return t
return GenericAlias(t.__origin__, stripped_args)
return t


Expand All @@ -1391,7 +1401,7 @@ def get_origin(tp):
"""
if isinstance(tp, _AnnotatedAlias):
return Annotated
if isinstance(tp, _BaseGenericAlias):
if isinstance(tp, (_BaseGenericAlias, GenericAlias)):
return tp.__origin__
if tp is Generic:
return Generic
Expand All @@ -1411,7 +1421,7 @@ def get_args(tp):
"""
if isinstance(tp, _AnnotatedAlias):
return (tp.__origin__,) + tp.__metadata__
if isinstance(tp, _BaseGenericAlias):
if isinstance(tp, (_BaseGenericAlias, GenericAlias)):
res = tp.__args__
if tp.__origin__ is collections.abc.Callable and res[0] is not Ellipsis:
res = (list(res[:-1]), res[-1])
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.