Repository navigation
Describe the overload a call matches in its call signature - #37
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:build_call_signaturebinds the arguments and runscheck_types_impl, which marks the overloads that don't accept them. It then tookbindings.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 underCallSignatureInfo.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 positionalmapoverload.The return type now agrees with ty's type for the call node far more often:
returnTypeIddiffers from the node's typeThe 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
Unknownargument, 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.CallableBinding::return_type()for that case was tried. On the same corpus, 92 of its 101 changes replaced a concrete return type withUnknown, 13 of them where ty types the call concretely.call_signature_is_the_overload_the_call_matchesfails without the fix (intwherestris expected).