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

Pylance only expects enum item string as second parameter #114

Closed
tweakimp opened this issue Jul 14, 2020 · 3 comments
Closed

Pylance only expects enum item string as second parameter #114

tweakimp opened this issue Jul 14, 2020 · 3 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

@tweakimp
Copy link

tweakimp commented Jul 14, 2020

Environment data

  • Language Server version: Pylance language server 2020.7.1
  • OS and version: Windows 10 64bit
  • Python version: 3.8.2

Expected behaviour

No error message and maybe even type hints about which kind of arguments enum.Enum can take.
See the docs.

names: The Enum members. This can be a whitespace or comma separated string (values will start at 1 unless otherwise specified):

'RED GREEN BLUE' | 'RED,GREEN,BLUE' | 'RED, GREEN, BLUE'
or an iterator of names:

['RED', 'GREEN', 'BLUE']
or an iterator of (name, value) pairs:

[('CYAN', 4), ('MAGENTA', 5), ('YELLOW', 6)]
or a mapping:

{'CHARTREUSE': 7, 'SEA_GREEN': 11, 'ROSEMARY': 42}

Actual behaviour

Message Expected enum item string as second parameter while code below is working as intended.

Code Snippet / Additional information

import enum
x = {"a":1, "b":2}

Thing = enum.Enum("Name", x.items()) # pylance marks error: Expected enum item string as second parameter
print(Thing.a.value) # prints 1
@erictraut
Copy link
Contributor

Thanks for the bug report.

If you pass a dynamic expression as the second argument to Enum() (as you have done in the code above), the type checker will have no knowledge of the enumerated values. You won't receive any completion suggestions, and Pylance won't be able to detect and report any incorrect usage. So, of the half dozen different ways to specify an enum in Python, I strongly recommend using the more standard class declaration rather than the forms you mentioned above.

class Name(Enum):
   a = 1
   b = 2

I think it's worth keeping the error in place but making it conditional on the reportGeneralTypeErrors rule. That way, it won't appear if you have typeCheckingMode set to "off". If you are interested in type checking, then it's important to know that switching to the more standard syntax for Enums will provide better results.

@erictraut
Copy link
Contributor

This change will be included in the next version of Pylance / Pyright.

@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 labels Jul 14, 2020
@jakebailey
Copy link
Member

This issue has been fixed in version 2020.7.2, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202072-15-july-2020

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