-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
mypy shows a somewhat confusing error message for functions returning a TypeVar value #15724
Comments
The correct type for this case would be: from typing import Callable, TypeVar, Optional
CallbackResult = TypeVar("CallbackResult")
VisitCallback = Callable[[str], Optional[CallbackResult]]
def visititems(self, func: VisitCallback[CallbackResult]) -> Optional[CallbackResult]:
return func("test") |
Yes, as I said above, I am aware of that. This issue is about the error message mypy should be showing. |
This warning message you met was not raised by not using Specifying In other words, |
Ok so alias I'm still not sure whether this is actually what was going on, because adding the "missing" Optional in the return actually removed the mypy error. If your explanation is correct, shouldn't it remain mismatched until I provide the typevar? Also, isn't it the case that usually leaving our the type parameters yields more "lenient"/"coarse" checking? But in any case, thanks for pointing out the interaction of |
My understanding is that when defining a type alias, the usage of I am not a mypy user because I am using pyright. Pyright still raises this warning even if Furthermore, I find the following interesting thing: from typing import TypeVar, Optional
CallbackResult = TypeVar("CallbackResult")
def visititems() -> Optional[CallbackResult]:
return None The above codes will not raise any error with mypy. But pyright can recognize the misusage correctly. I think maybe mypy has a loophole here. |
Yes, I think that I can fix the case where warning is gone when |
See discussion of |
Completes a `TODO` item :) Refs #15724 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Not sure if this should be considered a bug, or an enhancement proposal...
I had some code looking like this:
Actual Behavior
mypy says about this code:
Expected Behavior
First, I was confused, and thought that mypy is not able to infer that
CallbackResult
is hidden in the function input as the return value ofVisitCallback
. Actually I was initially going to open an issue because of that.Then I noticed that in a sense, mypy is correct - because I spotted that
VisitCallback
returnsOptional[CallbackResult]
, while thevisititems
function returns aCallbackResult
.Once I fixed that, the error went away. But in this case, mypy did not really "pinpoint" the problem as well as I feel it could.
Not sure how deeply mypy plays out the type algebra to figure out what types are constructible in a context, but I feel like this specific case could be a rather "common" one (forgetting the
Optional
), so maybe this can be catched with a better error message.Maybe something of the form "the function returns typevar value X, but based on its inputs can only construct Y[X, ...]" could be possible? So that it would cover
Optional, Union
and other simple cases likeList
, etc.Your Environment
The text was updated successfully, but these errors were encountered: