-
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
TypeVar yields an error when used with reduce #4614
Comments
Pyright is correct to emit an error here. You're using a TypeVar that is bound to def reduce(function: Callable[[_T, _T], _T], sequence: Iterable[_T]) -> _T:
... That means the two inputs to the callable must be the same type as the output. In your case, the inputs are type Not surprisingly, mypy generates the same error for this code. Here's how to adjust your code so it type checks without error: T = TypeVar("T")
def foo(x: Iterable[list[T]]) -> list[T]:
return reduce(lambda a, b: a + b, x) |
I'm sorry I didn't see this and reported a false positive!! 🙇🏻♂️ |
👉🏻 Describe the bug
When analyzing the code from below, I get:
This seems to happen because, the output of
reduce
is correctly parsed as alist[int]
, but then that type would be different fromlist[T]
, even ifT
is bound tolist[int]
👉🏻 To Reproduce
Put the code below in VSCode + pylance version
v2023.7.20
👉🏻 Expected behavior
If a
TypeVar
is bound to a type (even if it's a composition usingUnion
etc...), Pylance should consider another type valid if it matches with the bound of suchTypeVar
👉🏻 Code
👉🏻 VS Code extension or command-line
Running pylance in VSCode as an extension!
The text was updated successfully, but these errors were encountered: