Since Python 3.4 is adding support for enums to the standard library,
I wrote a TypeDecorator for it:
import sqlalchemy.types as types
class PythonEnum(types.TypeDecorator):
impl = types.Enum
def __init__(self, enum_class, **kw):
super().__init__(*(
m.name for m in enum_class), **kw)
self._enum_class = enum_class
def process_bind_param(self, value, dialect):
return
value.name
def process_result_value(self, value, dialect):
return self._enum_class[value]
@property
def python_type(self):
return self._enum_class
Comments welcome. Is this something that should be added to sqlalchemy?