Skip to content

Commit 9045919

Browse files
bpo-43772: Fix TypeVar.__ror__ (GH-25339)
1 parent 5224336 commit 9045919

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

Lib/test/test_typing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ def test_union_unique(self):
186186
self.assertEqual(Union[X, int].__parameters__, (X,))
187187
self.assertIs(Union[X, int].__origin__, Union)
188188

189+
def test_or(self):
190+
X = TypeVar('X')
191+
# use a string because str doesn't implement
192+
# __or__/__ror__ itself
193+
self.assertEqual(X | "x", Union[X, "x"])
194+
self.assertEqual("x" | X, Union["x", X])
195+
# make sure the order is correct
196+
self.assertEqual(get_args(X | "x"), (X, ForwardRef("x")))
197+
self.assertEqual(get_args("x" | X), (ForwardRef("x"), X))
198+
189199
def test_union_constrained(self):
190200
A = TypeVar('A', str, bytes)
191201
self.assertNotEqual(Union[A, str], Union[A])

Lib/typing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,8 @@ def __init__(self, bound, covariant, contravariant):
648648
def __or__(self, right):
649649
return Union[self, right]
650650

651-
def __ror__(self, right):
652-
return Union[self, right]
651+
def __ror__(self, left):
652+
return Union[left, self]
653653

654654
def __repr__(self):
655655
if self.__covariant__:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed the return value of ``TypeVar.__ror__``. Patch by Jelle Zijlstra.

0 commit comments

Comments
 (0)