Skip to content

bpo-43772: fix TypeVar.__ror__ #25339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2021
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
10 changes: 10 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ def test_union_unique(self):
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])
Comment on lines +193 to +194
Copy link
Member

@Fidget-Spinner Fidget-Spinner Apr 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these are testing for what Larry meant, Union comparisons are order independent, so

>>> T | "x" == "x" | T
True
>>> T | "x" == Union["x" | T]
True

And the current test should pass even without the patch.

What he meant is that the repr is order dependent, so the test should be something like:

Suggested change
self.assertEqual(X | "x", Union[X, "x"])
self.assertEqual("x" | X, Union["x", X])
self.assertEqual(repr(X | int), "typing.Union[~X, int]")
self.assertEqual(repr(int | X), "typing.Union[int, ~X]")

Which currently fails without the patch:

>>> repr(T | int)
"typing.Union[~T, int]"
>>> repr(int | T)
"typing.Union[int, ~T]"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I should have written the test first to make sure I was actually fixing anything.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made it use get_args() to test though, which feels cleaner than testing the repr() directly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that works :). The __repr__ is dependent on __args__ after all.

# 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])
Expand Down
4 changes: 2 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ def __init__(self, bound, covariant, contravariant):
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__:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the return value of ``TypeVar.__ror__``. Patch by Jelle Zijlstra.