Skip to content

Describe the overload a call matches in its call signature - #37

Merged
knutwannheden merged 1 commit into
mainfrom
match-call-signature-overload
Dec 4, 2026
Merged

knutwannheden merged 1 commit into
mainfrom
match-call-signature-overload

Conversation

@knutwannheden

Copy link
Copy Markdown
Contributor

Every call to an overloaded function got the first overload's signature in callSignature, whatever the call matched. rewrite-python builds method types from that field, so it attributed both calls below the same way:

@overload
def conv(x: int) -> str: ...
@overload
def conv(x: str) -> int: ...

conv("1")           # callSignature: (x: int) -> str   — expected (x: str) -> int
d: dict[str, int]
d.get("k", "x")     # callSignature: (key, default: None = None) -> int | None

build_call_signature binds the arguments and runs check_types_impl, which marks the overloads that don't accept them. It then took bindings.iter_flat().flatten().next(), which is overload 0 whatever that check found.

The change takes the first overload CallableBinding::matching_overloads() yields after type checking. When no overload accepts the arguments, it still describes the first overload, as before. The README states the rule under CallSignatureInfo.

Effect on a corpus

Run over pydantic (397 files, 43,411 call signatures), the old and new binaries differ on 3,934 calls. In spot checks, path.open("wb") now gets the binary-mode overload, and a TypedDict built from keywords no longer gets the positional map overload.

The return type now agrees with ty's type for the call node far more often:

calls whose returnTypeId differs from the node's type
before 4,449
after 2,663

The field still means "the resolved signature at the call site", so this is a fix, not a contract change. Only calls to an overloaded function, or to a union whose first member rejects the arguments, get a different signature.

Not changed here

  • When ty expands a union argument across overloads, or several overloads match an Unknown argument, ty types the call as something no single overload declares. The signature keeps the matched overload's return type, so parameters and return type describe one overload.
  • Taking CallableBinding::return_type() for that case was tried. On the same corpus, 92 of its 101 changes replaced a concrete return type with Unknown, 13 of them where ty types the call concretely.
  • A call that passes the arity check for one overload but fails its type check falls back to overload 0, not the overload ty reports the error against. That index is private to ty.

call_signature_is_the_overload_the_call_matches fails without the fix (int where str is expected).

build_call_signature type-checked the call's bindings but then took the
first overload regardless of the result, so every call to an overloaded
function carried the first overload's parameters and return type:
conv("1") against `conv(x: int) -> str` / `conv(x: str) -> int` read
`(x: int) -> str`, and dict.get("k", "x") read the `default: None`
overload.

Take the first overload that matched after type checking, falling back
to the first overload only when none did.
@knutwannheden knutwannheden self-assigned this Dec 4, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Dec 4, 2026
@knutwannheden
knutwannheden merged commit 1e79ba1 into main Dec 4, 2026
7 checks passed
@knutwannheden
knutwannheden deleted the match-call-signature-overload branch December 4, 2026 04:47
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant