-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1129,6 +1129,7 @@ def check_overload_call(self, | |
| erased_targets = None # type: Optional[List[CallableType]] | ||
| unioned_result = None # type: Optional[Tuple[Type, Type]] | ||
| unioned_errors = None # type: Optional[MessageBuilder] | ||
| union_success = False | ||
| if any(isinstance(arg, UnionType) and len(arg.relevant_items()) > 1 # "real" union | ||
| for arg in arg_types): | ||
| erased_targets = self.overload_erased_call_targets(plausible_targets, arg_types, | ||
|
|
@@ -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. | ||
| 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. | ||
|
||
| inferred_result = self.infer_overload_return_type(plausible_targets, args, arg_types, | ||
| arg_kinds, arg_names, callable_name, | ||
| object_type, context, arg_messages) | ||
| if inferred_result is not None: | ||
| # Success! Stop early. | ||
| return inferred_result | ||
| # Success! Stop early by returning the best among normal and unioned. | ||
| if not union_success: | ||
| return inferred_result | ||
| else: | ||
| assert unioned_result is not None | ||
| if is_subtype(inferred_result[0], unioned_result[0]): | ||
| return inferred_result | ||
| return unioned_result | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 (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.) |
||
|
|
||
| # Step 4: Failure. At this point, we know there is no match. We fall back to trying | ||
| # to find a somewhat plausible overload target using the erased types | ||
|
|
||
There was a problem hiding this comment.
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"