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

Support all (or at least more) forms of the functional API for Enum creation #1127

Closed
tmke8 opened this issue Oct 28, 2020 · 6 comments
Closed
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request

Comments

@tmke8
Copy link

tmke8 commented Oct 28, 2020

Is your feature request related to a problem? Please describe.

I'd like to use this form of Enum creation:

B = Enum('B', ['p', 'q', 'r'])

but it is currently not supported by pyright.

A full list of allowed forms can be found here: python/mypy#2306

Describe the solution you'd like
I'd like pyright to accept the above code and to know about the members B.p, B.q and B.r. This is the behavior that mypy has.

Additional context
Pyright already supports this form:

B = Enum('B', 'p q r'])

but I think this is visually not as easy to parse.

@erictraut
Copy link
Collaborator

There's already a much cleaner way to define enums if you have static enum labels, so I recommend you use that instead of these alternate forms. These alternate forms are generally only useful if your enum labels are dynamic. And in those cases, a static type checker won't be able to understand them anyway.

We could theoretically handle a specific case where you're using a list expression with string literals (as opposed to a variable that contains a list or a list constructor call), but it would involve a bunch of special-case code. I don't think it's worth it.

I'm going to close this as "won't fix", but other users can feel free to upvote or comment, and we might reconsider this decision in the future.

@matangover
Copy link

These alternate forms are generally only useful if your enum labels are dynamic.

I disagree. It's useful to specify an enum as a one-liner without having to worry about the values.

@GBeauregard
Copy link

data point: mypy considered this a high priority issue python/mypy#2306

If you're in strict mode pyright's errors are a bit harder to work around, but it might have only been high priority for mypy cause they error'd in default mode

heejaechang added a commit to heejaechang/pyright that referenced this issue Nov 3, 2021
* change n^2 linear search to map

this improves 260ms to 70ms on my test env. improvement will be even bigger for longer auto import list.

* addressed PR feedback
@Kache
Copy link

Kache commented Apr 12, 2023

Hoping pyright will also support the tuple/mapping syntax too!

MyEnum = Enum('MyEnum', {'HA': 'ha', 'FOO': 'bar'})

@erictraut erictraut reopened this Apr 12, 2023
@erictraut erictraut added the enhancement request New feature or request label Apr 12, 2023
erictraut pushed a commit that referenced this issue Apr 15, 2023
…ings, a tuple of strings, a list of tuples, a tuple of tuples, and a map. This addresses #1127.
@erictraut
Copy link
Collaborator

I've added support for the following Enum functional forms:

  • Whitespace-separated names in a string: Enum("name", "a b c")
  • Comma-separated names in a string: Enum("name", "a,b,c")
  • List of names: Enum("name", ["a", "b", "c"])
  • Tuple of names: Enum("name", ("a", "b", "c"))
  • List of tuples: Enum("name", [("a", 1), ("b", 2), ("c", 3)])
  • Tuple of tuples: Enum("name", (("a", 1), ("b", 2), ("c", 3)))
  • Map: Enum("name", {"a": 1, "b": 2, "c": 3})

This will be included in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Apr 15, 2023
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.304, which is now published. It will also be included in a future release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants