bpo-43772: Fix TypeVar.__ror__ (GH-25339)

This commit is contained in:
Jelle Zijlstra 2021-04-10 20:00:05 -07:00 committed by GitHub
parent 522433601a
commit 9045919bfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 2 deletions

View File

@ -186,6 +186,16 @@ class TypeVarTests(BaseTestCase):
self.assertEqual(Union[X, int].__parameters__, (X,))
self.assertIs(Union[X, int].__origin__, Union)
def test_or(self):
X = TypeVar('X')
# use a string because str doesn't implement
# __or__/__ror__ itself
self.assertEqual(X | "x", Union[X, "x"])
self.assertEqual("x" | X, Union["x", X])
# make sure the order is correct
self.assertEqual(get_args(X | "x"), (X, ForwardRef("x")))
self.assertEqual(get_args("x" | X), (ForwardRef("x"), X))
def test_union_constrained(self):
A = TypeVar('A', str, bytes)
self.assertNotEqual(Union[A, str], Union[A])

View File

@ -648,8 +648,8 @@ class _TypeVarLike:
def __or__(self, right):
return Union[self, right]
def __ror__(self, right):
return Union[self, right]
def __ror__(self, left):
return Union[left, self]
def __repr__(self):
if self.__covariant__:

View File

@ -0,0 +1 @@
Fixed the return value of ``TypeVar.__ror__``. Patch by Jelle Zijlstra.