Improve Step 5 of overload call evaluation.#2250
Open
rchen152 wants to merge 1 commit intopython:mainfrom
Open
Improve Step 5 of overload call evaluation.#2250rchen152 wants to merge 1 commit intopython:mainfrom
rchen152 wants to merge 1 commit intopython:mainfrom
Conversation
Makes two modifications to step 5 of overload call evaluation: 1. When materializing arguments to check whether we can eliminate overloads, skip arguments that have the same parameter type in all overloads. 2. When we still have multiple overloads after the materialization filter, try to find a return type that all materializations of all other return types are assignable to. Updates the conformance tests as well. All type checkers already do (1). Mypy and pyrefly pass the conformance test for (2). I believe mypy has a heuristic that approximates the new rule; pyrefly implements it exactly.
Member
|
I think it's preferable to keep PRs updating type-checker versions separate from PRs implementing conformance suite and spec changes. |
| - All possible :term:`materializations <materialize>` of the argument's type are | ||
| assignable to the corresponding parameter type, or | ||
| - The parameter types corresponding to this argument in all of the remaining overloads | ||
| are :term:`equivalent`. |
Member
There was a problem hiding this comment.
I think this is correct but there's some subtlety in the wording that we should cover in the test cases:
- "The parameter types corresponding to this argument". That may be a very different parameter in each of the overloads; e.g. maybe one takes
*argsand the other has explicit arguments. So my reading is that to check this, you have to create for each overload a mapping between argument and parameter type, and consult that. What is the parameter type corresponding to an unpacked argument, though? - "the remaining overloads". So we only check this overload and all following overloads, not ones above it?
carljm
reviewed
Apr 8, 2026
| def example5(x: A[Any]) -> A[Any]: ... | ||
|
|
||
| def test(x: Any): | ||
| # Steps 5 eliminates the first overload because there exists a |
Member
There was a problem hiding this comment.
Suggested change
| # Steps 5 eliminates the first overload because there exists a | |
| # Step 5 eliminates the first overload because there exists a |
|
|
||
|
|
||
| @overload | ||
| def example8(x: str, y: Literal['o1']) -> str: ... # E? some type checkers report unsafe overlap between example8's overloads |
Member
There was a problem hiding this comment.
This unnecessary complexity would be eliminated (without reducing the strength of the test) by changing str here to bool (and changing the rest of the test accordingly).
|
|
||
|
|
||
| def check_example9(x: Any): | ||
| # Steps 5 eliminates the first overload because there exists a |
Member
There was a problem hiding this comment.
Suggested change
| # Steps 5 eliminates the first overload because there exists a | |
| # Step 5 eliminates the first overload because there exists a |
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.
Makes two modifications to step 5 of overload call evaluation:
Updates the conformance tests as well. All type checkers already do (1). Mypy and pyrefly pass the conformance test for (2). I believe mypy has a heuristic that approximates the new rule; pyrefly implements it exactly. I updated pyrefly to a version that contains its implementation of (2), which also picked up some unrelated changes.