-
Notifications
You must be signed in to change notification settings - Fork 768
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
Incorrect inferred type of returned value of Callable
that has a generic Optional
return type
#1462
Comments
Thanks for the bug report. There is logic in the type evaluator that handles a union return type that includes both concrete types and one or more generic types that are not "solved" when the call expression is evaluated. This is important in cases like this: def func(a: List[int | T]) -> int | T:
...
reveal_type(func(["hi"])) # int | str
# In this case, "T" remains unsolved, and we don't want the resulting
# type to be `int | T` because `T` has no meaning outside of the scope
# of the callee. For that reason, the `T` is dropped from the resulting type.
reveal_type(func([3])) # int This logic resulted in the type of However, in this example, This will be fixed in the next release. |
This issue has been fixed in version 2021.6.3, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202163-23-june-2021 |
I'm afraid the bug is still not solved completely. The code that originally produced the bug is a little more complex than the sample I provided here. This other sample produces the same effect: from typing import Optional, TypeVar
from collections.abc import Callable
T = TypeVar("T")
U = TypeVar("U")
def h(x: T, f: Callable[[T], Optional[U]]) -> U:
def g() -> U:
y = f(x)
if y is not None:
return y
raise ValueError()
return g() |
Yep, my previous fix didn't account for nested scopes. I've updated it to handle that case as well. |
Many thanks for your rapidity! Also, I have encountered another problem that might be related to this bug but I'm not totally sure. I was waiting for today's release to see if it would fix it but apparently it didn't. Let me know if you think I should open another issue. The snippet is this: from typing import (
Any,
Callable,
Optional,
TypeVar,
Union,
)
T = TypeVar("T")
U = TypeVar("U")
def g(f: Callable[[Any], Optional[T]]) -> T:
...
def h(t1: type[T], t2: type[U]) -> Union[T, U]:
def f(x: Any) -> Union[T, U]:
if isinstance(x, (t1, t2)):
return x
raise ValueError()
x = g(f)
return x The type of |
Please file a new issue in the pyright repo, and I'll take a look at it. On first inspection, mypy also evaluates the type as |
Environment data
Expected behaviour
The infered type of
y
shoud beU | None
.Actual behaviour
The inferred type of the local variable
y
in the function listed below isNone
. Because of this, whithin theif y is not None:
the inferred type becomesNever
and if I returny
PyLance marks it as an error.Logs
Code Snippet / Additional information
The text was updated successfully, but these errors were encountered: