Skip to content

Enum.__members__ not recognized #1590

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

Closed
FichteFoll opened this issue Sep 6, 2017 · 6 comments
Closed

Enum.__members__ not recognized #1590

FichteFoll opened this issue Sep 6, 2017 · 6 comments

Comments

@FichteFoll
Copy link
Contributor

FichteFoll commented Sep 6, 2017

Despite the update in #1195, I still get an error when accessing an Enum's __members__ attribute with mypy 0.521 and Python 3.6.2:

import enum


class NetworkEventName(str, enum.Enum):
    CONNECTED = "connected"
    DISCONNECTED = "disconnected"
    CLOSE_REQUEST = "close_request"
    MESSAGE = "message"
    RAW_LINE = "raw_line"


print(NetworkEventName.__members__)
> mypy /tmp/enum_test.py
/tmp/enum_test.py:12: error: Type[NetworkEventName] has no attribute "__members__"

> python /tmp/enum_test.py
OrderedDict([('CONNECTED', <NetworkEventName.CONNECTED: 'connected'>), ('DISCONNECTED', <NetworkEventName.DISCONNECTED: 'disconnected'>), ('CLOSE_REQUEST', <NetworkEventName.CLOSE_REQUEST: 'close_request'>), ('MESSAGE', <NetworkEventName.MESSAGE: 'message'>), ('RAW_LINE', <NetworkEventName.RAW_LINE: 'raw_line'>)])

@property
def __members__(self: Type[_T]) -> Mapping[str, _T]: ...

@JelleZijlstra
Copy link
Member

I wonder if this is a mypy bug, since the property is present on the metaclass in typeshed. cc @elazarg who implemented a lot of the metaclass support in mypy.

@elazarg
Copy link
Contributor

elazarg commented Sep 6, 2017

It seems to happen due to a spurious "inconsistent metaclass structure".

str implements Sequence, and therefore has ABC in its MRO. This means NetworkEventName has both EnumMeta and ABCMeta in the meta-mro, neither of which is a subclass of the other. This is the pretty much same problem as with IntEnum.
It is easy to work around in mypy, but I really hope it will be possible to avoid it using Protocols. The ad-hoc special casing is not easy to do consistently and maintainably. Until then, I suggest making Enum inherit from ABCMeta if possible.
If the above suggestions are not possible, I can submit a PR to address this specific case.

@elazarg
Copy link
Contributor

elazarg commented Sep 7, 2017

(so this is not a mypy bug, in my opinion)

@elazarg
Copy link
Contributor

elazarg commented Sep 8, 2017

In essence, this is duplicate of the more general python/mypy#2824

@FichteFoll
Copy link
Contributor Author

Seems like it, indeed. I'm closing this then.

@elazarg
Copy link
Contributor

elazarg commented Sep 8, 2017

Even more general: #1595.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants