You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
A=Enum('A', 'x y z')
B=Enum('B', ('p', 'q'))
These are equivalent to
classA(Enum):
x=1y=2z=3classB(Enum):
p=1q=2
Some other syntactic variants are also allowed:
A=Enum('A', 'x,y,z')
A=Enum('A', 'x, y, z')
B=Enum('B', ['p', 'q'])
The values can also be expressed as a list of tuples:
I've been checking out mypy for less than two minutes, stumbled upon the error (Too many arguments for "Enum") with the first few files to check, searched the web, and followed a trail of issues to find out that this is at least known.
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 # type: ignore to silence the message.
@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).
@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.
from enum import IntEnum
from typing import Collection
def list_to_enum(cls, fields: Collection[str]) -> IntEnum:
""" Convert a list of textual fields to the class:`IntEnum` that needs
to be stored in this class :attr:`fields` """
return IntEnum(
cls._FIELDS_ENUM_NAME, dict(zip(fields, range(len(fields))))
)
The second argument is the source of enumeration member names. It can be a whitespace-separated string of names, a sequence of names, a sequence of 2-tuples with key/value pairs, or a mapping (e.g. dictionary) of names to values. The last two options enable assigning arbitrary values to enumerations; the others auto-assign increasing integers starting with 1 (use the start parameter to specify a different starting value). A new class derived from Enum is returned.
Activity
homeworkprod commentedon Jan 31, 2017
I've been checking out mypy for less than two minutes, stumbled upon the error (
Too many arguments for "Enum") with the first few files to check, searched the web, and followed a trail of issues to find out that this is at least known.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)?
JukkaL commentedon Jan 31, 2017
@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
# type: ignoreto silence the message.@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).
gvanrossum commentedon Jan 31, 2017
Agreed.
homeworkprod commentedon Jan 31, 2017
@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 `Enum` functional API calls on static type checking
Support functional API for Enum.
Support functional API for Enum.
Support functional API for Enum.
Support functional API for Enum.
3 remaining items
Support functional API for Enum.
Support functional API for Enum.
Support functional API for Enum.
Support functional API for Enum.
Support functional API for Enum.
Support functional API for Enum. (#2805)
homeworkprod commentedon May 25, 2017
Just a quick feedback: Works for me now (tested with Mypy 0.511). Thanks!
anentropic commentedon Nov 1, 2017
mypy 0.540
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?
anentropic commentedon Nov 1, 2017
ah I see, removing the
type=str, module=__name__extra kwargs gets rid of the second error tooI 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
JukkaL commentedon Nov 1, 2017
@anentropic Yeah, this is something mypy should be able to support pretty easily. Created #4184 to track the issue.
rdbisme commentedon Feb 27, 2020
Is this related? #4865
Because I'm hitting the same problem...
From here
JukkaL commentedon Feb 28, 2020
@rubendibattista Your example is too dynamic for mypy to understand (see #4865 (comment)).
mvoitko commentedon Oct 15, 2021
Are there PRs, issues or at least plans to handle the case @rubendibattista metioned?
I have faced a similar problem: