How to fix Python enum 'TypeError: Error when calling the metaclass bases: module.__init__() takes at most 2 arguments (3 given)'

Problem:

You want to inherit a Python class from enum like this:

broken_enum_example.py
import enum

class MyEnum(enum):
    X = 1
    Y = 2class

but when you try to run it, you see an error message like this:

enum_traceback.txt
Traceback (most recent call last):
    File "test.py", line 3, in <module>
        class MyEnum(enum):
TypeError: Error when calling the metaclass bases
        module.__init__() takes at most 2 arguments (3 given)

Solution

You are not trying to inherit from the Enum class (capital E!) but from the enum module!

This is the correct syntax:

fixed_enum_example.py
from enum import Enum
class MyEnum(Enum):
    X = 1
    Y = 2

Check out similar posts by category: Python