8000 Problems with Enums: iteration, __new__ · Issue #3622 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
Problems with Enums: iteration, __new__ #3622
Closed
@ilinum

Description

@ilinum

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0