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

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented Apr 11, 2021

A very simple change.

https://bugs.python.org/issue43772

Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

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

Thanks! I just have a comment about the tests

Comment on lines +188 to +189
self.assertEqual(X | "x", Union[X, "x"])
self.assertEqual("x" | X, Union["x", X])
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.

Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

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

LGTM.

@gvanrossum gvanrossum merged commit 9045919 into python:master Apr 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants