Skip to content

Choose best type when working with unioned overloads #5242

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 4 commits into from
Jun 20, 2018

Conversation

ilevkivskyi
Copy link
Member

Fixes #5240

This is a simple straightforward fix.

@ilevkivskyi ilevkivskyi requested a review from Michael0x2a June 19, 2018 17:33
Copy link
Collaborator

@Michael0x2a Michael0x2a left a comment

Choose a reason for hiding this comment

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

Just a few minor nitpicks, but otherwise LGTM!

# Success! Stop early.
return unioned_result
# Success! But we need to see maybe normal procedure gives a narrower type.
union_success = True

# Step 3: If the union math fails, or if there was no union in the argument types,
# we fall back to checking each branch one-by-one.
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably tweak this comment -- maybe just something simple like

# Step 3: We try checking each branch one-by-one

?

I don't think it matters too much, as long as we remove the "if the union math fails" bit.

@@ -1143,17 +1144,23 @@ def check_overload_call(self,
callable_name=callable_name,
object_type=object_type)
if not unioned_errors.is_errors():
# Success! Stop early.
return unioned_result
# Success! But we need to see maybe normal procedure gives a narrower type.
Copy link
Collaborator

Choose a reason for hiding this comment

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

"need to see maybe normal procedure" -> "need to see if maybe the normal procedure"

# Success! Stop early.
return unioned_result
# Success! But we need to see maybe normal procedure gives a narrower type.
union_success = True
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can simplify this if statement to union_success = not unioned_errors.is_errors(). We'd also need to change the comment above to # Record if we succeeded. Next we need to see if maybe...

assert unioned_result is not None
if is_subtype(inferred_result[0], unioned_result[0]):
return inferred_result
return unioned_result
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, just had another thought -- I think it would be clearer (and a little more efficient) if we added logic to return early if union_success is true somewhere around here. Currently, we return early only if the normal algorithm also succeeds.

(The logic currently does the right thing because step 5 happens to do what we want, but step 4 and down are supposed to be failure cases.)

@ilevkivskyi ilevkivskyi merged commit 5dc7454 into python:master Jun 20, 2018
@ilevkivskyi ilevkivskyi deleted the fix-overload-43 branch June 20, 2018 13:53
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.

Union math is too greedy
2 participants