Skip to content

Relax union math logic to allow signatures with different arg kinds #5222

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
Jun 16, 2018

Conversation

Michael0x2a
Copy link
Collaborator

Resolves #5204.

This commit relaxes the union math logic so that it allows signatures with arg kinds that are nearly identical except that one arg is positional and the other is optional.

This commit also removes the "names must be the same" restriction mostly so that the original example given in #4576 will pass. (In retrospect, I think this check didn't really buy us much -- even if the alternatives share the same arg names, there's no guarantee the actual implementation signature will also share the same.)

Resolves python#5204.

This commit relaxes the union math logic so that it allows signatures
with arg kinds that are nearly identical except that one arg is
positional and the other is optional.

This commit also removes the "names must be the same" restriction mostly
so that the original example given in python#4576
will pass. (In retrospect, I think this check didn't really buy us much
-- even if the alternatives share the same arg names, there's no
guarantee the actual implementation signature will also share the same.)
Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good. Feel free to merge after addressing the comments.

@@ -2173,7 +2173,7 @@ def f(x): ...
x: Union[A, B]
reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.C'
f(x) # E: Argument 1 to "f" has incompatible type "Union[A, B]"; expected "A"
reveal_type(f(x)) # E: Revealed type is 'Union[__main__.B, __main__.C]'
Copy link
Member

Choose a reason for hiding this comment

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

The test case name should be updated (remove "Skip").


x: Union[A, B]
f(x) # E: Argument 1 to "f" has incompatible type "Union[A, B]"; expected "A"
f(x, B()) # E: Argument 1 to "f" has incompatible type "Union[A, B]"; expected "B"
Copy link
Member

Choose a reason for hiding this comment

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

I think ideally we could support something like this. Could you please add a TODO here? (I see you have a TODO in code, but I would prefer to have them both).

@Michael0x2a
Copy link
Collaborator Author

Thanks! Merging now.

@Michael0x2a Michael0x2a merged commit e66d53b into python:master Jun 16, 2018
@Michael0x2a Michael0x2a deleted the enhance-union-math-logic branch June 16, 2018 01:10
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.

2 participants