Skip to content

Problems with Enums: iteration, __new__ #3622

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
ilinum opened this issue Jun 27, 2017 · 7 comments
Closed

Problems with Enums: iteration, __new__ #3622

ilinum opened this issue Jun 27, 2017 · 7 comments
Labels
bug mypy got something wrong priority-1-normal

Comments

@ilinum
Copy link
Collaborator

ilinum commented Jun 27, 2017

Consider the following perfectly valid python code:

from enum import IntEnum, Enum

class Colors(IntEnum):
    RED = 0
    BLUE = 1

class Colors2(int, Enum):  # E: Definition of "__new__" in base class "int" is incompatible with definition in base class "Enum"
    GREEN = 0
    PURPLE = 1

for color in Colors:  # E: Type[Colors] has no attribute "__iter__"
    print(color.name)

for color in Colors2:  # E: Type[Colors2] has no attribute "__iter__"
    print(color.name)

This probably has to do with support for metaclasses. Here is the output from mypy and python.

$ mypy e.py
e.py:7: error: Definition of "__new__" in base class "int" is incompatible with definition in base class "Enum"
e.py:11: error: Iterable expected
e.py:11: error: Type[Colors] has no attribute "__iter__"
e.py:14: error: Iterable expected
e.py:14: error: Type[Colors2] has no attribute "__iter__"
$ mypy e.py --py2
e.py:7: error: Definition of "__new__" in base class "int" is incompatible with definition in base class "Enum"
e.py:11: error: Iterable expected
e.py:11: error: Type[Colors] has no attribute "__iter__"
e.py:14: error: Iterable expected
e.py:14: error: Type[Colors2] has no attribute "__iter__"
$ python3 e.py
RED
BLUE
GREEN
PURPLE
$ python e.py
RED
BLUE
GREEN
PURPLE
@ilinum ilinum added the bug mypy got something wrong label Jun 27, 2017
@gvanrossum
Copy link
Member

The __new__ problem is probably separate; the lack of iteration is because IntEnum doesn't have the right metaclass (EnumMeta), and trying to add it gives a different error:

__tmp__.py:8: error: Inconsistent metaclass structure for 'Colors2'

@ilevkivskyi
Copy link
Member

This seems to be partially related to #3210, and as I mentioned there, this may be solved easily by structural subtyping. However, the __new__ part looks harder to fix.

@ilinum
Copy link
Collaborator Author

ilinum commented Jul 7, 2017

The __new__ issue seems to be addressed here: python/typeshed#1464

(Haven't had time to test it to make sure though)

@JelleZijlstra
Copy link
Member

Oh right, this is a near-duplicate of the typeshed issue I opened. I think there may be more regressions here since the last mypy release, so I'll look into this more (hopefully tonight).

@JukkaL JukkaL changed the title Derived Enums cause problems Problems with Enums: iteration, __new__ Sep 6, 2017
@elazarg
Copy link
Contributor

elazarg commented Sep 8, 2017

I believe this is a duplicate of #2824

@elazarg
Copy link
Contributor

elazarg commented Sep 8, 2017

More general: python/typeshed#1595

@emmatyping
Copy link
Member

Closing as a duplicate of #2824

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal
Projects
None yet
Development

No branches or pull requests

7 participants