]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45489: Update ForwardRef to support | operator. (GH-28991)
authorDong-hee Na <donghee.na@python.org>
Sat, 16 Oct 2021 15:12:58 +0000 (00:12 +0900)
committerGitHub <noreply@github.com>
Sat, 16 Oct 2021 15:12:58 +0000 (00:12 +0900)
Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2021-10-16-23-46-39.bpo-45489.QB0rhG.rst [new file with mode: 0644]

index 38397a07e5717957f005376937d98158a31659c8..032fe91c7a8408b8396998c983259f09e1502d77 100644 (file)
@@ -2903,6 +2903,12 @@ class ForwardRefTests(BaseTestCase):
         self.assertNotEqual(gth(Loop, globals())['attr'], Final[int])
         self.assertNotEqual(gth(Loop, globals())['attr'], Final)
 
+    def test_or(self):
+        X = ForwardRef('X')
+        # __or__/__ror__ itself
+        self.assertEqual(X | "x", Union[X, "x"])
+        self.assertEqual("x" | X, Union["x", X])
+
 
 class OverloadTests(BaseTestCase):
 
index ada9adb0d32a58c13ae5541fb3c5a19509fe7f74..78d973d2bba056f5501cd7a2d008c64427cbc6c2 100644 (file)
@@ -719,6 +719,12 @@ class ForwardRef(_Final, _root=True):
     def __hash__(self):
         return hash(self.__forward_arg__)
 
+    def __or__(self, other):
+        return Union[self, other]
+
+    def __ror__(self, other):
+        return Union[other, self]
+
     def __repr__(self):
         return f'ForwardRef({self.__forward_arg__!r})'
 
diff --git a/Misc/NEWS.d/next/Library/2021-10-16-23-46-39.bpo-45489.QB0rhG.rst b/Misc/NEWS.d/next/Library/2021-10-16-23-46-39.bpo-45489.QB0rhG.rst
new file mode 100644 (file)
index 0000000..3921437
--- /dev/null
@@ -0,0 +1,2 @@
+Update :class:`~typing.ForwardRef` to support ``|`` operator. Patch by
+Dong-hee Na.