-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Returning T instead of UserDefinedClass[T] is not reported as an error #2905
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
This is because def f(v: Node) -> Node:
reveal_type(v.value) # E: Revealed type is 'Any'
return v.value This is why mypy has def f(v: Node) -> Node:
return v.value # W: Returning Any from function with declared return type "x.Node[Any]" In this case you probably wanted to define the function as def f(v: Node[T]) -> Node[T]:
return v.value # E: Incompatible return value type (got "T", expected Node[T]) and mypy would find an error. |
Ah that makes sense. Maybe |
Opps, sorry The only remaining issue is that |
Looks like a bug to me. This check is only done in def g(v: Node[T]) -> Iterator[Node[T]]:
yield v.value is catched. I see another issue with def g(v: Node) -> Iterator[Node]:
yield v.value
return v.value but |
Agreed. With regards to the new issue @miedzinski raised: you shouldn't really be returning a value there anyway, as it's unusable. Feel free to file a separate issue about it, though. (Closing as a duplicate of #2901.) |
At first glance, the following code should not pass type verification because
f
andg
return or yieldT
instead ofNode[T]
:However,
mypy --strict --python-version 3.6
doesn't report any errors. It does produce a warning:but even the warning is only reported for
f()
, not forg()
.To be fair, there are ways to call these functions that doesn't violate type expectations, for example
f(Node(Node(1)))
would returnNode(1)
which fits its promised return type. But I was hopingmypy
might notice that the only way this code would be correct is if the argument tof()
is aNode[Node]
rather than just arbitraryNode
. And so I was expectingmypy
to force me to write:if that's what I meant of course. (Although it's much more likely that it's actually a bug, and that I meant to
return v
rather thanreturn v.value
; in that case,mypy
would help me catch that bug.)Furthermore, if I add the following code at the end of the original code snippet:
mypy
should be able to see that something is definitely wrong; at this point it's completely clear thatf(v)
won't return the promisedNode
, andg(v)
won't yield one. But adding those lines does not add any new error or warning messages.The text was updated successfully, but these errors were encountered: