Skip to content

Commit

Permalink
Make Union[list[T], int] work
Browse files Browse the repository at this point in the history
This involves a rare change in typing.py.
  • Loading branch information
gvanrossum committed Mar 15, 2020
1 parent f658a64 commit bfd6342
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
3 changes: 1 addition & 2 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ def test_union_generic(self):
T = typing.TypeVar('T')
a = typing.Union[list[T], tuple[T, ...]]
self.assertEqual(a.__args__, (list[T], tuple[T, ...]))
# TODO: To make this work, would need to update typing.py to recognize list[T].
# self.assertEqual(a.__parameters__, (T,))
self.assertEqual(a.__parameters__, (T,))


if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import re as stdlib_re # Avoid confusion with the re we export.
import sys
import types
from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType
from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias

# Please keep __all__ alphabetized within each category.
__all__ = [
Expand Down Expand Up @@ -180,7 +180,8 @@ def _collect_type_vars(types):
for t in types:
if isinstance(t, TypeVar) and t not in tvars:
tvars.append(t)
if isinstance(t, _GenericAlias) and not t._special:
if ((isinstance(t, _GenericAlias) and not t._special)
or isinstance(t, GenericAlias)):
tvars.extend([t for t in t.__parameters__ if t not in tvars])
return tuple(tvars)

Expand Down

0 comments on commit bfd6342

Please sign in to comment.