[Python-checkins] bpo-45489: Update ForwardRef to support | operator. (GH-28991)
corona10
webhook-mailer at python.org
Sat Oct 16 11:13:07 EDT 2021
https://github.com/python/cpython/commit/15ad52fbf607b6ccec44a38a8a32a5f1fad635ee
commit: 15ad52fbf607b6ccec44a38a8a32a5f1fad635ee
branch: main
author: Dong-hee Na <donghee.na at python.org>
committer: corona10 <donghee.na92 at gmail.com>
date: 2021-10-17T00:12:58+09:00
summary:
bpo-45489: Update ForwardRef to support | operator. (GH-28991)
files:
A Misc/NEWS.d/next/Library/2021-10-16-23-46-39.bpo-45489.QB0rhG.rst
M Lib/test/test_typing.py
M Lib/typing.py
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 38397a07e5717..032fe91c7a840 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -2903,6 +2903,12 @@ def test_final_forward_ref(self):
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 ada9adb0d32a5..78d973d2bba05 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -719,6 +719,12 @@ def __eq__(self, other):
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 0000000000000..3921437d97ac5
--- /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.
More information about the Python-checkins
mailing list