diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-07-22 22:18:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-22 22:18:49 (GMT) |
commit | 21db59fc75b6ebb01bf120a8e5930fe032174f73 (patch) | |
tree | 70d2d39828355586ec055a1b42ca349947445479 /Lib | |
parent | 9608719e12b14087c1cb34f77c847974442cd6e7 (diff) | |
download | cpython-21db59fc75b6ebb01bf120a8e5930fe032174f73.zip cpython-21db59fc75b6ebb01bf120a8e5930fe032174f73.tar.gz cpython-21db59fc75b6ebb01bf120a8e5930fe032174f73.tar.bz2 |
bpo-44653: Support typing types in parameter substitution in the union type. (GH-27247) (#27296)
(cherry picked from commit 2e3744d50b6e30ea24351e55b4352dcc58fd469e)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_types.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 6ed7471..99b011e 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -772,6 +772,36 @@ class UnionTests(unittest.TestCase): self.assertEqual((list[T] | list[S])[int, T], list[int] | list[T]) self.assertEqual((list[T] | list[S])[int, int], list[int]) + def test_union_parameter_substitution(self): + def eq(actual, expected): + self.assertEqual(actual, expected) + self.assertIs(type(actual), type(expected)) + + T = typing.TypeVar('T') + S = typing.TypeVar('S') + NT = typing.NewType('NT', str) + x = int | T | bytes + + eq(x[str], int | str | bytes) + eq(x[list[int]], int | list[int] | bytes) + eq(x[typing.List], int | typing.List | bytes) + eq(x[typing.List[int]], int | typing.List[int] | bytes) + eq(x[typing.Hashable], int | typing.Hashable | bytes) + eq(x[collections.abc.Hashable], + int | collections.abc.Hashable | bytes) + eq(x[typing.Callable[[int], str]], + int | typing.Callable[[int], str] | bytes) + eq(x[collections.abc.Callable[[int], str]], + int | collections.abc.Callable[[int], str] | bytes) + eq(x[typing.Tuple[int, str]], int | typing.Tuple[int, str] | bytes) + eq(x[typing.Literal['none']], int | typing.Literal['none'] | bytes) + eq(x[str | list], int | str | list | bytes) + eq(x[typing.Union[str, list]], typing.Union[int, str, list, bytes]) + eq(x[str | int], int | str | bytes) + eq(x[typing.Union[str, int]], typing.Union[int, str, bytes]) + eq(x[NT], int | NT | bytes) + eq(x[S], int | S | bytes) + def test_union_parameter_substitution_errors(self): T = typing.TypeVar("T") x = int | T |