Add description to an inherited field inside a pydantic object

Viewed 19

I would like to add a custom description to an enum field. I tried the following code

from enum import Enum
from pydantic import BaseModel


class BasicEnum(Enum):
    e1 = "E1"
    e2 = "E2"


class ModifiedEnum(BasicEnum):
    """
    This is really an enum!!!
    """


class PydandicObject(BaseModel):
    enum_field: ModifiedEnum

However, this returns the following error

Traceback (most recent call last):
  File "tmp_enum.py", line 9, in <module>
    class ModifiedEnum(BasicEnum):
  File "C:\Users\lazlo\AppData\Local\Programs\Python\Python38\lib\enum.py", line 146, in __prepare__
    metacls._check_for_existing_members(cls, bases)
  File "C:\Users\lazlo\AppData\Local\Programs\Python\Python38\lib\enum.py", line 527, in _check_for_existing_members
    raise TypeError(
TypeError: ModifiedEnum: cannot extend enumeration 'BasicEnum'
0 Answers
Related