-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Fix inconsistent type resolution with unused type variables #20541
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
base: master
Are you sure you want to change the base?
Conversation
|
@A5rocks @hauntsaninja would you review my PR? glad to contribute on this project! |
for more information, see https://pre-commit.ci
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
A5rocks
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really get what _any_in_unrelated_position is doing and I'm not convinced I should spend the hour it would take to do so :P
What's the logic for it?
| # So it's safe to just append everything to the same list. | ||
| for formal in formals: | ||
| matching_formals.append(matched_callable.arg_types[formal]) | ||
| if not all_same_types(matching_formals) and not all_same_types(matching_returns): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack, I did in fact misunderstand.
But I feel like this is probably wrong, rather than having a complicated addition to patch things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is your suggestion? we should have complicated addition? @A5rocks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be great if you give me the advice in case my direction is wrong.. 😉
Thank you for your time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'm not sure! I'm a bit confused about what this code is supposed to do, because I don't think the formals being the same type is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.

Fixes #20442
Problem
When a generic class has an unused type parameter, mypy incorrectly returns
Anywhen resolving overloads involving functions that reference that class.Reproduction
Root Cause
When
DataFramehas an unused type parameterIndexT0, mypy treats bareDataFrameasDataFrame[Any]. ThisAnypropagates into callable types likeCallable[[DataFrame[Any]], list[Hashable]].The
any_causes_overload_ambiguity()function incheckexpr.pywas too aggressive - it saw the nestedAnyin the argument and concluded there was ambiguity, even though theAnywas in a position that was identical across all overload formals (the callable's argument type), not in the position that differentiated the overloads (the callable's return type).Solution
Added
_any_in_unrelated_position()function that checks whetherAnyin an argument is in a position that doesn't affect overload differentiation:Changes
mypy/checkexpr.py: Added_any_in_unrelated_position()helper function and integrated it intoany_causes_overload_ambiguity()test-data/unit/check-overloading.test: Added two regression test casesTest Plan
testOverloadWithNestedAnyInCallableDoesNotCauseAmbiguity- tests direct callable argumentstestOverloadWithNestedAnyInTupleCallableDoesNotCauseAmbiguity- tests tuple containing callable arguments (matches the original issue)Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=42954461