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

Variable explicit type being ignored #993

Closed
BrunoBlanes opened this issue Feb 26, 2021 · 9 comments
Closed

Variable explicit type being ignored #993

BrunoBlanes opened this issue Feb 26, 2021 · 9 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

@BrunoBlanes
Copy link

Environment data

  • Pylance Type Checking Mode: basic
  • Language Server version: 2021.3.600354918-dev
  • OS and version: Windows 10 Pro Insider Preview Build 21313
  • Python version (& distribution if applicable, e.g. Anaconda): 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 as Any.

Actual behavior

Variable is being interpreted as Any. The same happens if I use guild: 'Guild | None'.

guild: Optional[Guild] = ctx.bot.get_guild(529775376721903617)
if guild is not None:
    guild # type is Any

Additional information

get_guild() is indeed untyped and returns Any | None, but I expected the interpreter to prioratize my explicitly set Guild type over Any.

issue

@jakebailey
Copy link
Member

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.

@erictraut
Copy link
Contributor

Types are narrowed upon assignment. An Any on the RHS causes the type of the assignment target to be narrowed to Any.

If you want to explicitly cast an expression's type to a specified type, you need to use the typing.cast call.

@BrunoBlanes
Copy link
Author

But is it wrong to have it the other way? I only see it as an improvement. Using typing.cast means an additional import statement and less readable code.

@erictraut
Copy link
Contributor

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.

@erictraut
Copy link
Contributor

My statement above ("An Any on the RHS causes the type of the assignment target to be narrowed to Any") was incorrect. I was confusing it with the case where the LHS contained an Any.

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 Any — precisely the situation in your example.

This will be fixed 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 Feb 26, 2021
@BrunoBlanes
Copy link
Author

Hey, do you mind me asking when can I expect to see this fix in the Insider build?

@erictraut
Copy link
Contributor

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.

@jakebailey
Copy link
Member

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

@BrunoBlanes
Copy link
Author

SWEET, IT WORKS!

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