Author ethan.furman
Recipients JEphron, ethan.furman
Date 2021-06-10.15:33:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1623339238.36.0.483930413508.issue44356@roundup.psfhosted.org>
In-reply-to
Content
Since I like puzzles, here is a working LenientStrEnum:

    class LenientStrEnum(str, Enum):
        #
        def __init__(self, *args):
            self._valid = True
        #
        @classmethod
        def _missing_(cls, value):
            logger.warning(
                f"[{cls.__name__}] encountered an unknown value!\n"
                f"Luckily I'm a LenientStrEnum, so I won't crash just yet.\n"
                f"You might want to add a new case though.\n"
                f"Value was: '{value}'"
                )
            unknown = cls._member_type_.__new__(cls, value)
            unknown._valid = False
            unknown._name_ = value.upper()
            unknown._value_ = value
            cls._member_map_[value] = unknown
            return unknown
        #
        @property
        def valid(self):
            return self._valid

`_member_map_` is not guaranteed, but is unlikely to change.
History
Date User Action Args
2021-06-10 15:33:58ethan.furmansetrecipients: + ethan.furman, JEphron
2021-06-10 15:33:58ethan.furmansetmessageid: <1623339238.36.0.483930413508.issue44356@roundup.psfhosted.org>
2021-06-10 15:33:58ethan.furmanlinkissue44356 messages
2021-06-10 15:33:58ethan.furmancreate