-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Enum-like user classes #4311
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Have you tried inheriting from |
Can this be achieved by descriptors? mypy already supports |
@ethanhs: I did try deriving from @ilevkivskyi: Not sure what you mean. My problem is in convincing |
You can make |
Ah, I see. This would require writing |
I think it is a reasonable feature request to make instances of EnumMeta have enum-like types. |
I feel that the underlying issue @orenbenkiki is hitting is #1020, since Also, I'm not certain that we should support classes with |
I wasn't talking about inheriting classes but classes that use it as metaclass; this is a declaration of intent just like inheriting Enum. |
Ah, I mispoke, I mean classes that use it as a metaclass. Anyway, I don't see why we can't try that. |
I have implemented it - it's pretty easy. But it falsifies the assumption on typeshed that only Enum derivatives instantiate EnumClass, so typeshed should be fixed first. |
This is needed for python/mypy#4311, and is sound.
This is needed for python/mypy#4311, and is sound.
@orenbenkiki I am going to close this, as the original ask is unlikely to happen, and |
Oh, I thought that had been merged! My mistake. |
Feature request: Allow annotating a class as enum-like for
mypy
type checking purposes.I have classes dealing with a value-like (immutable) type.
One of the classes in that family is an enum-like class, whose values are of the new immutable type.
I tried to implement this by deriving from the standard library
Enum
, but it has pretty strong opinions about the implementation which do not match the way my value type works (it is too different from a plain integer).No worries, I just implemented my own
MyEnumBase
base class (andMyEnumMeta
for it), loosely inspired byEnum
.It works fine.
The problem is convincing
mypy
to type check it.Specifically, if I write:
Then
mypy
will not recognize thatEnumExample.FOO
has the typeEnumExample
.I can work around this by the following horrible hack:
Which mostly works, but, shudder (is there a better way to achieve the same effect?)
It would be nice if I could annotate
MyEnumBase
somehow to letmypy
it is an enum base class.For example, if the single
mypy.nodes.ENUM_BASECLASS
was replaced by a set of class names, so that it were possible to insert additional class names into it (and provide a command line flag tomypy
for doing that).The text was updated successfully, but these errors were encountered: