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

Class variable as type alias #1043

Closed
timrid opened this issue Mar 13, 2021 · 2 comments
Closed

Class variable as type alias #1043

timrid opened this issue Mar 13, 2021 · 2 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

@timrid
Copy link

timrid commented Mar 13, 2021

Environment data

  • Language Server version: Pylance v2021.3.1
  • OS and version: Windows 10
  • Python version: Python 3.8.6

Actual behaviour

The following code generates an error for Color2, because it is not recognised as type alias. But Color1 is correctly recognised as type alias, and generates no error.

Code Snippet / Additional information

from typing import Literal

# ----------------------------------
Color1 = Literal[1, 2]
class Window1():
    def get_color(self) -> Color1: ...

c1 = Window1().get_color()
reveal_type(c1)

# ----------------------------------
class Window2():
    Color2 = Literal[3, 4]
    def get_color(self) -> Color2: ...

c2 = Window2().get_color()
reveal_type(c2)

image

@erictraut
Copy link
Contributor

Thanks for the bug report. This will be fixed in the next release of pylance.

Type aliases in Python are tricky because they use the same syntax as variable assignments. A type checker needs to apply various heuristics to to tell them apart. (PEP 613 introduces a way to explicitly annotate type aliases, but implicit type aliases must still be supported.) This issue was the result of one of those heuristics that needed to be adjusted to accommodate type aliases defined within a class with literal types.

@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 Mar 13, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.3.2, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202132-17-march-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