Skip to content

Report bad self in overloaded method calls as bad-argument-type - #5663

Closed
OverloadedTech wants to merge 3 commits into
pyrefly-org:mainfrom
OverloadedTech:fix-overload-bad-self
Closed

OverloadedTech wants to merge 3 commits into
pyrefly-org:mainfrom
OverloadedTech:fix-overload-bad-self

Conversation

@OverloadedTech

@OverloadedTech OverloadedTech commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Calling an overloaded method on a receiver that doesn't match self reported
no-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 into accepts_receiver):

  • only the overloads that accept the receiver are matched against the arguments
  • if just one accepts it, that overload is called like a regular method
  • if none accept it, the receiver is reported once as bad-argument-type, as for a
    non-overloaded method, and the arguments are still matched against all overloads
  • receivers with unsolved vars skip this step

Fixes #5217

  • I am a human, and I fully understand the code I am submitting.

(This description was drafted with Claude and reviewed by me.)

Behaviour changes in existing tests:

  • test_dict_update: dict[int, int].update(a=1) is now no-matching-overload,
    because the **kwargs overloads need str keys and are filtered out
  • test_intersection_call_representative_lambda_context: v + 1 with v: str now
    evaluates to str (only str.__add__(self, value: str) applies), so B no
    longer accepts the lambda and the result is RetA instead of RetA & RetB

Test Plan

  • test_overload_error_bad_self_argument covers the no-overload, some-overloads
    and mixed cases
  • cargo test -p pyrefly --lib -- test::overload:: test::decorators::
    test::typed_dict:: test::intersection_calls:: test::generic_basic:: (610 passed)

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 stroxler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_infer checks 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

@stroxler stroxler self-assigned this Nov 16, 2026
@OverloadedTech

OverloadedTech commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review @stroxler ! I think that makes sense, handling the receiver first
is cleaner than patching things up after the overload matching fails.
I'll rework it: filter the overloads by receiver, report the error once
if none of them match, then check the actual arguments.

Just a thing: if no overload accepts the receiver, should I still match
the arguments against all the overloads so a bad argument gets reported
too? Apart from that i think were all set

@stroxler

Copy link
Copy Markdown
Contributor

Yes, match the arguments against all the overloads, but leave the receiver out of the matching. The flow can be something like this:

  • Filter by receiver first. Drop the overloads whose self rejects the receiver. If any remain, match the arguments against only those, so the receiver doesn't participate anymore.
  • If none accept it, report one bad-argument-type for self, worded as for a plain method. Then match arguments against all overloads with the receiver set aside:
    b.meth(1) → only the self error, and the call gets that overload's return type (int).
    b.meth(1.5) → the self error, plus no-matching-overload for (float). Its details should mention only a, with no "B is not assignable to self" line.
  • Reuse the existing filter. filter_overloads_by_receiver in alt/function.rs already does step 1 for new/init. It's better to build on it than add a fourth receiver check.

@OverloadedTech

OverloadedTech commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Thank you @stroxler , that makes sense! I reworked it that way and pushed (also merged
the latest main):

  • pulled the per-signature check out of filter_overloads_by_receiver into
    accepts_receiver, and used it in the BoundMethodOverload arm of
    call_infer_with_callee_range, so there's no new receiver check
  • if some overloads accept the receiver, only those get matched. If just
    one is left, it's called like a normal method (so "a" + 1 still gives
    unsupported-operation instead of losing the operator context)
  • if none accept it, the receiver is reported once, then the arguments are
    matched against all overloads without it. b.meth(1) gives just the self
    error and int, b.meth(1.5) gives the self error plus no-matching-overload
    for (float) that only mentions a
  • receivers with unsolved vars skip this, so d = {}; d.update(a=1) still
    infers dict[str, int]

Two existing tests changed, wanted to check you're ok with them:

  • test_dict_update: dict[int, int].update(a=1) is now no-matching-overload,
    since the **kwargs overloads need str keys and get filtered out
  • test_intersection_call_representative_lambda_context: with only
    str.add(self, value: str) left, v + 1 with v: str now gives str
    instead of Unknown, so B's method no longer accepts the lambda and the
    result is RetA instead of RetA & RetB

@meta-codesync

meta-codesync Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

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.)

@stroxler

Copy link
Copy Markdown
Contributor

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

@OverloadedTech

OverloadedTech commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Perfect, it's a pleasure working with you @stroxler and the community

@stroxler stroxler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@meta-codesync

meta-codesync Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in e1afe02.

@OverloadedTech
OverloadedTech deleted the fix-overload-bad-self branch November 19, 2026 06:33
@OverloadedTech

Copy link
Copy Markdown
Contributor Author

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Confusing error message for bad-argument-type on methods where self is incorrect and there are overloads

2 participants