]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43772: Fix TypeVar.__ror__ (GH-25339)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Sun, 11 Apr 2021 03:00:05 +0000 (20:00 -0700)
committerGitHub <noreply@github.com>
Sun, 11 Apr 2021 03:00:05 +0000 (20:00 -0700)
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2021-04-10-19-14-49.bpo-43772.Bxq0zQ.rst [new file with mode: 0644]

index 7183686a6dcde3c1e3fe341e2beb72f9f3049381..82c517a4e600248d881275374ec5bce14c0a4ec4 100644 (file)
@@ -186,6 +186,16 @@ class TypeVarTests(BaseTestCase):
         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)
         self.assertNotEqual(Union[A, str], Union[A])
index 6461ba23dd7e2535683c59df2ed72f7bc3c965ce..a24c01f0e3b9e364f259d8d9ee72099ba10e0992 100644 (file)
@@ -648,8 +648,8 @@ class _TypeVarLike:
     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 (file)
index 0000000..648357b
--- /dev/null
@@ -0,0 +1 @@
+Fixed the return value of ``TypeVar.__ror__``. Patch by Jelle Zijlstra.