Skip to content

Enum types cannot be inherited #10857

Description

@sobolevn

Feature

Subtyping with Enum exact values

This core currently passes mypy:

fromenumimportEnumclassA(Enum):
x=1classB(A):
...

But, in runtime this code fails with:

Traceback (most recent call last):
File "ex.py", line 6, in <module>
class B(A):
File "/Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/enum.py", line 146, in __prepare__
metacls._check_for_existing_members(cls, bases)
File "/Users/sobolev/.pyenv/versions/3.8.9/lib/python3.8/enum.py", line 527, in _check_for_existing_members
raise TypeError(
TypeError: B: cannot extend enumeration 'A'

This is also the case for Flag, IntEnum, and IntFlag.

Subtyping without exact values

However, subtyping without Enum values is fine:

fromenumimportEnumclassA(Enum):
...
classB(A):
...

This is also the case for Flag, IntEnum, and IntFlag.

So, the rule is: types inherited from Enum with at least one enumeration value should be marked as implicitly final type.

Related #10852
Related #5599

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions