-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
False positive for @overload and type variable #5407
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
Comments
I think this error message is correct, though it unfortunately doesn't give a lot of clues why. Basically, the issue is that if we try applying the two overloads in order, the second one will never be matched. For example, if we try calling While mypy technically doesn't need to flag this, having overloads that can never match is probably not what a user wants, so mypy reports an error. The two workarounds are either to:
Probably option 1 will result in the most understandable code in your specific case. |
Is there any action item for us here or we can just close this? (This seems to behave this way by design.) |
@ilevkivskyi -- the error message could probably be improved. Maybe add an extra line that says We could either track this here or close this and add another TODO to the general overloads tracking post, up to you. |
@Michael0x2a thanks for the explanation. Swapping fixes the issue. Can you clarify why this error happens when using |
@mxr -- so, when you call an overloaded function, mypy will check each possible variant in the order you defined them and use whatever variant ended up matching first. This error occurs when it ends up being the case that it's impossible for some variant to ever be selected. For example, in your TypeVar example the second overload will never be selected, no matter what. If we pass in any two arbitrary types X and Y as arguments into This is not the case for your list example: the arguments (Note: Older versions of mypy did use to consider |
OK, we can keep this one open. I will label this as usability issue. |
@Michael0x2a I see, thanks. I think my biggest misunderstanding was that "we can infer |
This issue seems similar to #4619. My environment:
Example file:
mypy
fails with:If I replace
T
with an explicit type such asList[str]
then
mypy
passesThe text was updated successfully, but these errors were encountered: