Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter overload items based on self type during type inference #17873

Merged
merged 11 commits into from
Oct 3, 2024

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Oct 3, 2024

Fix type argument inference for overloaded functions with explicit self types. Filter out the overload items based on the declared and actual types of self. The implementation is best effort and does the filtering only in simple cases, to reduce the risk of regressions (primarily performance, but I worry also about infinite recursion). I added a fast path for the typical case, since without it the filtering was quite expensive.

Note that the overload item filtering already worked in many contexts. This only improves it in specific contexts -- at least when inferring generic protocol compatibility.

This is a more localized (and thus lower-risk) fix compared to #14975 (thanks @tyralla!). #14975 might still be a good idea, but I'm not comfortable merging it now, and I want a quick fix to unblock the mypy 1.12 release.

Fixes #15031. Fixes #17863.

Co-authored by @tyralla.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Oct 3, 2024

I added a test case from #14975 by @tyralla.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Yeah, looks hacky, but OK as a quick fix.

This comment has been minimized.

Copy link
Contributor

github-actions bot commented Oct 3, 2024

Diff from mypy_primer, showing the effect of this PR on open source code:

discord.py (https://github.com/Rapptz/discord.py)
+ discord/ext/commands/help.py:668: error: Argument "key" to "sort" of "list" has incompatible type "Callable[[Command[Any, [VarArg(Any), KwArg(Any)], Any]], Any] | None"; expected "Callable[[Command[Any, [VarArg(Any), KwArg(Any)], Any]], SupportsDunderLT[Any] | SupportsDunderGT[Any]]"  [arg-type]

scipy (https://github.com/scipy/scipy)
- scipy/_lib/unuran/get_and_clean_unuran.py:55: error: Cannot infer type argument 1 of "copyfileobj"  [misc]

@JukkaL
Copy link
Collaborator Author

JukkaL commented Oct 3, 2024

The new error in discord.py seems correct. We are now filtering the overloads of list.sort as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression: IO[str] not compatible with SupportsWrite[AnyStr] shutil.copyfileobj stopped working in 1.2.0
2 participants