Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
bpo-45489: Update ForwardRef to support | operator.
  • Loading branch information
corona10 committed Oct 16, 2021
commit 0bb525a3abd491a1353fe78df6bb2c18152c3104
6 changes: 6 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
6 changes: 6 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Update :class:`~typing.ForwardRef` to support ``|`` operator. Patch by
Dong-hee Na.