-
Notifications
You must be signed in to change notification settings - Fork 767
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
Variable explicit type being ignored #993
Comments
FWIW I thought #822 meant that we'd be taking the left hand side as truth, but that doesn't appear to be what's happening. |
Types are narrowed upon assignment. An If you want to explicitly cast an expression's type to a specified type, you need to use the |
But is it wrong to have it the other way? I only see it as an improvement. Using |
Upon further investigation, we appear to be inconsistent here. from typing import Any, List, Optional
def func1(a: Any, b: Optional[Any]):
x: int = a
reveal_type(x) # int
y: Optional[int] = b
reveal_type(y) # Optional[Any]
z: List[int] = [a]
reveal_type(z) # List[int] I'll investigate further. |
My statement above ("An I found the cause of the inconsistency. It is triggered only in the case where the declared type of the assignment LHS is a union and the type of the expression on the assignment RHS is also a union that contains an This will be fixed in the next release. |
Hey, do you mind me asking when can I expect to see this fix in the Insider build? |
We release new versions of Pylance once a week, typically on Wednesday. This fix will be enabled in the public version of the next release. |
This issue has been fixed in version 2021.3.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202130-3-march-2021 |
SWEET, IT WORKS! |
Environment data
basic
2021.3.600354918-dev
Windows 10 Pro Insider Preview Build 21313
3.8.7 64-bit
Expected behavior
As it was briefly discussed at microsoft/vscode-python#15487, even though I have explicitly set
guild: Optional[Guild]
it is still interpreted asAny
.Actual behavior
Variable is being interpreted as
Any
. The same happens if I useguild: 'Guild | None'
.Additional information
get_guild()
is indeed untyped and returnsAny | None
, but I expected the interpreter to prioratize my explicitly setGuild
type overAny
.The text was updated successfully, but these errors were encountered: