I want to write an Enum that inherits from collections.abc.Set
with the sole purpose of implementing the required abstract methods. The documentation suggests 3 ways of doing it and I can get it to work using option 2:
collections.abc
— Abstract Base Classes for Containers
- Existing classes and built-in classes can be registered as “virtual subclasses” of the ABCs
But I think the Enum would look better in an API using option 1) as having the collections.abc.Set
in the class signature is more explicit... So I tried this:
import collections
from enum import Enum
class MyClass(collections.abc.Set, Enum):
AB = {1}
CD = {1,2}
def __iter__(self):
pass
def __contains__(self, key):
pass
def __len__(self):
pass
And the expectable error was thrown:
class MyClass(collections.abc.Set, Enum):
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
How would a solution using option 1) look like? (It's been asked a few times on SO, namely in this question, but not with a minimal canonical example of inheriting from a collections.abc
class just to override the abstract methods in the class definition.)