-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 functional API for Enum creation #2306
Comments
I've been checking out mypy for less than two minutes, stumbled upon the error ( Yeah, I'd like to see this fixed rather sooner than later. This might drive new users away right at the start. Until then, is there a way to suppress the error and keep mypy from exiting with a return code > 0 (for use in CI scripts)? |
@homeworkprod The recommended fix is to refactor your code to use an alternative enum syntax. Is that not feasible for some reason? If not, I'd like to hear about your concerns. You can also try adding @gvanrossum What do you think about making this a high priority issue? Multiple users seem to be hitting this, and this shouldn't be hard to support (though the implementation will be pretty tedious to write). |
Agreed. |
@JukkaL Well, I highly prefer the (oftentimes) one-liner functional API to spreading the definition of a (smaller) enum over multiple lines and including distracting, somewhat error-prone numbers that are irrelevant for the business logic (in the cases in question). Why have (to read) half a page of code when three lines neatly do the trick? The ignore hint works for me, so I'll go with that for now, thank you. |
Ignore hints can be removed after python/mypy#2306 gets resolved.
Fixes #2306. Also move Enum tests from runtests to pytest.
Just a quick feedback: Works for me now (tested with Mypy 0.511). Thanks! |
mypy 0.540 PictureSize = Enum('PictureSize', 'P0 P1 P2 P3 P4 P5 P6 P7 P8', type=str, module=__name__) gives:
Later when trying to access a value from the enum:
I understand if it won't be fixed, but I thought from previous comment maybe it had been? |
ah I see, removing the I guess with this much working now it ought to be easy to add recognition of all the kwargs the constructor supports as detailed in the docs?https://docs.python.org/3/library/enum.html#functional-api |
@anentropic Yeah, this is something mypy should be able to support pretty easily. Created #4184 to track the issue. |
Is this related? #4865 Because I'm hitting the same problem...
From here
|
@rubendibattista Your example is too dynamic for mypy to understand (see #4865 (comment)). |
Are there PRs, issues or at least plans to handle the case @rubendibattista metioned? import enum
from typing import Any
class StrAutoEnum(str, enum.Enum):
def _generate_next_value_(name: str, start: Any, count: int, last_values: Any) -> str: # type: ignore[misc, override] # noqa:E501
return name
Target = StrAutoEnum('Target', {name: enum.auto() for name in model_names}) # type: ignore[call-overload] |
The
enum.Enum
class (new in Python 3.4, also backported as enum34) supports several short forms of creating an Enum type, described under functional API:These are equivalent to
Some other syntactic variants are also allowed:
The values can also be expressed as a list of tuples:
or a mapping:
The text was updated successfully, but these errors were encountered: