[Python] How to add deprecation warning to Python enum fields?

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    Given the following enum:

    class MyEnum(enum.Enum):
    Field1 = "Field1"
    Field2 = "Field2" # should be deprecated for `Field3`
    Filed3 = "Field3"


    I'd like to add a deprecation warning to a field such that when I call MyEnum.Field2 or MyEnum("Field2") a <MyEnum.Field3: 'Field3'> enum instance will be returned as well as a deprecation warning.

    What is the proper way to do so? Is there a python language feature that can do this?

    Continue reading...

Compartilhe esta Página