From 82f2c8655ab1e81e08ee3aeba636efbee0885115 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 10 Apr 2021 19:15:44 -0700 Subject: [PATCH 1/2] fix TypeVar.__ror__ --- Lib/test/test_typing.py | 2 ++ Lib/typing.py | 4 ++-- .../next/Library/2021-04-10-19-14-49.bpo-43772.Bxq0zQ.rst | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2021-04-10-19-14-49.bpo-43772.Bxq0zQ.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 3b8efe16c6e238..c4bb0fbf91763a 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -185,6 +185,8 @@ def test_union_unique(self): self.assertEqual(Union[X, int].__args__, (X, int)) self.assertEqual(Union[X, int].__parameters__, (X,)) self.assertIs(Union[X, int].__origin__, Union) + self.assertEqual(X | "x", Union[X, "x"]) + self.assertEqual("x" | X, Union["x", X]) def test_union_constrained(self): A = TypeVar('A', str, bytes) diff --git a/Lib/typing.py b/Lib/typing.py index 6224930c3b0275..9091cf62ed045a 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -646,8 +646,8 @@ def __init__(self, bound, covariant, contravariant): def __or__(self, right): return Union[self, right] - def __ror__(self, right): - return Union[self, right] + def __ror__(self, left): + return Union[left, self] def __repr__(self): if self.__covariant__: diff --git a/Misc/NEWS.d/next/Library/2021-04-10-19-14-49.bpo-43772.Bxq0zQ.rst b/Misc/NEWS.d/next/Library/2021-04-10-19-14-49.bpo-43772.Bxq0zQ.rst new file mode 100644 index 00000000000000..648357b3faa741 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-04-10-19-14-49.bpo-43772.Bxq0zQ.rst @@ -0,0 +1 @@ +Fixed the return value of ``TypeVar.__ror__``. Patch by Jelle Zijlstra. From dfa16a7cca3db92cb4fa926bcd95e1181ee75dc1 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 10 Apr 2021 19:31:00 -0700 Subject: [PATCH 2/2] make the test more useful --- Lib/test/test_typing.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index c4bb0fbf91763a..40f0be887bef72 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -185,8 +185,16 @@ def test_union_unique(self): self.assertEqual(Union[X, int].__args__, (X, int)) self.assertEqual(Union[X, int].__parameters__, (X,)) self.assertIs(Union[X, int].__origin__, Union) + + def test_or(self): + X = TypeVar('X') + # use a string because str doesn't implement + # __or__/__ror__ itself self.assertEqual(X | "x", Union[X, "x"]) self.assertEqual("x" | X, Union["x", X]) + # make sure the order is correct + self.assertEqual(get_args(X | "x"), (X, ForwardRef("x"))) + self.assertEqual(get_args("x" | X), (ForwardRef("x"), X)) def test_union_constrained(self): A = TypeVar('A', str, bytes)