Repository navigation
Report bad self in overloaded method calls as bad-argument-type - #5663
OverloadedTech wants to merge 3 commits into
Conversation
When every overload of a method expects the same `self` type and the receiver is the only argument that doesn't match, report the error the same way as for a non-overloaded method instead of listing the overloads.
stroxler
left a comment
There was a problem hiding this comment.
Hi @OverloadedTech, thanks for the PR!
I like the code structure here, pulling logic into a helper is very readable. But it seems like the fix might not be actually addressing the root cause. It appears that this PR adds new way for call_overloads to count as "matched for recovery", beside a real match and a unique arity match.
It might be better to instead attack the root cause head-on. The actual problem seems to be that:
- The receiver goes through call_overloads as self_obj.
callable_inferchecks it as positional argument 0 (at callable.rs:1291, 2651).- The mismatch therefore lands in each overload's call_errors and num_match_errors (overload.rs:80), so find_closest_overload rejects every overload.
- overload_error strips self from the display but attaches the self error as the explanation, and the result becomes Unknown.
Instead, we want this situation to give one bad-argument-type and infer None.
A way to accomplish this would be to run receiver binding as its own step before overload matching:
- filter overloads by receiver
- if none accept it, report the receiver error once in non-overloaded form
- then match the explicit arguments.
How does that sound to you? It will require bigger changes, but probably solves the problem by more directly addressing the root cause
|
Thanks for the review @stroxler ! I think that makes sense, handling the receiver first Just a thing: if no overload accepts the receiver, should I still match |
|
Yes, match the arguments against all the overloads, but leave the receiver out of the matching. The flow can be something like this:
|
|
Thank you @stroxler , that makes sense! I reworked it that way and pushed (also merged
Two existing tests changed, wanted to check you're ok with them:
|
|
This pull request has been imported. If you are a Meta employee, you can view this in D132517275. (Because this pull request was imported automatically, there will not be any future comments.) |
|
Thanks! It looks pretty good, but I'm going to delay merging until the 1.5 release is cut because this will introduce more churn for Pyrefly users (some error codes change from no-matching-override to bad-argument-type) Some churn for better analysis is fine in the long run, but 1.5 is already a really big release, and we're pretty close to putting it out so I don't want to complicate that process. I think the release will probably happen this week, and then I'll merge and this fix will go into 1.6 |
|
Perfect, it's a pleasure working with you @stroxler and the community |
stroxler
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
|
This pull request has been merged in e1afe02. |
|
Thanks |
Calling an overloaded method on a receiver that doesn't match
selfreportedno-matching-overload, listing every overload, with the real problem buried at
the bottom. The receiver was checked as an ordinary argument of each overload,
so it made every overload fail.
Now the receiver is checked before the arguments are matched, using the same
check as
filter_overloads_by_receiver(pulled out intoaccepts_receiver):non-overloaded method, and the arguments are still matched against all overloads
Fixes #5217
(This description was drafted with Claude and reviewed by me.)
Behaviour changes in existing tests:
dict[int, int].update(a=1)is now no-matching-overload,because the **kwargs overloads need str keys and are filtered out
v + 1withv: strnowevaluates to
str(onlystr.__add__(self, value: str)applies), so B nolonger accepts the lambda and the result is
RetAinstead ofRetA & RetBTest Plan
and mixed cases
test::typed_dict:: test::intersection_calls:: test::generic_basic:: (610 passed)