import enum from collections.abc import Callable from typing import Any import sqlalchemy as sa from sqlalchemy_utils.types.choice import ChoiceType as BaseChoiceType class EnumType(BaseChoiceType): # type: ignore[misc] cache_ok = True impl = sa.Text() def __init__( self, enum_class: type[enum.Enum], processor: Callable[[Any], enum.Enum | None] | None = None, ) -> None: super().__init__(choices=enum_class) self._processor = processor def process_result_value(self, value: Any, dialect: Any) -> Any: if self._processor: value = self._processor(value) return super().process_result_value(value, dialect)