Not sure if this would be in mypy or the typeshed, though it may need support in both.
Currently enum.IntEnum provides a way to have an enum whose value is an int, however all other enum.Enums have a .value of Any.
While this can be worked around in more recent versions of Python by adding a member without a value within the enum:
class MyEnum(str, enum.Enum):
value: str
FIRST = 'first'
This isn't ideal as it means that there are two places where this needs to be specified -- as an inherited type and within the class body.
It feels like it would be better if mypy was able to infer the member value type from the inheritance.
Aside: had generics existed before Enum, it feels like that might alternatively solved this, something like:
class MyEnum(enum.Enum[str]):
FIRST = 'first'
I think this could be handled by updating the enums plugin? (It might possibly need some supporting typeshed change also?)
If this to be added, maybe consider supporting this:
import enum
class Blah(int, enum.Enum):
foo = '5'
print(repr(Blah.foo.value)) # would print `5`
reveal_type(Blah.foo.value) # reveals "str" but should reveal "int"
# https://mypy-play.net/?mypy=latest&python=3.8&gist=46ca47b0d534d00f2218c8835783b8c2
I'm pretty sure this is an intended use of Enums? I'm not sure :P
@A5rocks that seems evil, I think mypy should flag that as an error.
Most helpful comment
@A5rocks that seems evil, I think mypy should flag that as an error.