summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_typing.py6
-rw-r--r--Lib/typing.py6
-rw-r--r--Misc/NEWS.d/next/Library/2021-10-16-23-46-39.bpo-45489.QB0rhG.rst2
3 files changed, 14 insertions, 0 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 38397a07e5..032fe91c7a 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -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):
diff --git a/Lib/typing.py b/Lib/typing.py
index ada9adb0d3..78d973d2bb 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -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
index 0000000000..3921437d97
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-10-16-23-46-39.bpo-45489.QB0rhG.rst
@@ -0,0 +1,2 @@
+Update :class:`~typing.ForwardRef` to support ``|`` operator. Patch by
+Dong-hee Na.