Skip to content
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

Type inference returns a type different from the declared type. #1802

Closed
sasano8 opened this issue Sep 11, 2021 · 5 comments
Closed

Type inference returns a type different from the declared type. #1802

sasano8 opened this issue Sep 11, 2021 · 5 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@sasano8
Copy link

sasano8 commented Sep 11, 2021

In the following example,
passing func3 will return str instead of the declared Literal["running"].
It should return the declared type as is.

from typing import Literal, Tuple, List, Sequence, TypeVar, Iterable

T = TypeVar("T")

def func1(val: Iterable[T]) -> T:
    ...

def func2(val: Sequence[T]) -> T:
    ...

def func3(val: Tuple[T]) -> T:
    ...


status: Tuple[Literal["running"]] = ("running",)

a = func1(status)  # Literal["running"] to Literal["running"]
b = func2(status)  # Literal["running"] to Literal["running"]
c = func3(status)  # Literal["running"] to str why?

Screenshot from 2021-09-11 12-12-53

@erictraut
Copy link
Contributor

Are you seeing a behavior that leads to a false positive or false negative type error, or are you just curious about the behavior you're seeing? If you are seeing an actual bug, please provide a sample that demonstrates the problem. If you're just curious, then posting a question in the discussion section would be more appropriate.

@sasano8
Copy link
Author

sasano8 commented Sep 11, 2021

I see this as a false positive type error.

I have only given it the type Literal["running"].
So, TypeVar T is Literal["running"].
And, str does not belong to Literal["running"].
This is because Literal["running"] is narrower than str.

So it contradicts the next sentence.

Return Type Inference
The returned type is assumed to be the union of all types returned from all return statements.

The str that should not have belonged to Literal["running"] has returned.

I can't indicate the problem any further, and I'm not interested in it, so if it's not a problem, please close it.

kind regards.

@erictraut
Copy link
Contributor

I agree that this is an inconsistency. I investigated and found the root cause. This inconsistency will be eliminated in the next release.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Sep 11, 2021
@sasano8
Copy link
Author

sasano8 commented Sep 12, 2021

Thank you!
I love VSCODE and I wish VSCODE further development.

@jakebailey
Copy link
Member

This issue has been fixed in version 2021.9.2, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202192-16-september-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants