serhii.net

In the middle of the desert you can say anything you want

10 Aug 2022

Huggingface utils ExplicitEnum python bits

In the Huggingface source found this bit:

class ExplicitEnum(str, Enum):
    """
    Enum with more explicit error message for missing values.
    """

    @classmethod
    def _missing_(cls, value):
        raise ValueError(
            f"{value} is not a valid {cls.__name__}, please select one of {list(cls._value2member_map_.keys())}"
        )

… wow?

(Pdb++) IntervalStrategy('epoch')
<IntervalStrategy.EPOCH: 'epoch'>
(Pdb++) IntervalStrategy('whatever')
*** ValueError: whatever is not a valid IntervalStrategy, please select one of ['no', 'steps', 'epoch']

Was MyEnum('something') allowed the whole time? God I feel stupid.

Nel mezzo del deserto posso dire tutto quello che voglio.