-
-
Notifications
You must be signed in to change notification settings - Fork 760
✨ Add IntEnum for sqltypes #1337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…nto feat/IntEnum
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KunxiSun, thanks for your interest and efforts!
I think this feature is quite useful.
There seems to be no simple solution to configure int enums this way without creating custom TypeDecorator
. This PR will provide such solution.
Please take a look at my in-code comments.
Also, we need to update these tests to check statements for new field
if value is None: | ||
return None | ||
|
||
result = self.enum_type(value) | ||
return result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if value is None: | |
return None | |
result = self.enum_type(value) | |
return result | |
return None if (value is None) else self.enum_type(value) |
if value is None: | ||
return None | ||
|
||
result = value.value | ||
return result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if value is None: | |
return None | |
result = value.value | |
return result | |
return None if (value is None) else value.value |
@@ -14,3 +17,39 @@ def load_dialect_impl(self, dialect: Dialect) -> "types.TypeEngine[Any]": | |||
if impl.length is None and dialect.name == "mysql": | |||
return dialect.type_descriptor(types.String(self.mysql_default_length)) | |||
return super().load_dialect_impl(dialect) | |||
|
|||
|
|||
class IntEnum(types.TypeDecorator): # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class IntEnum(types.TypeDecorator): # type: ignore | |
class IntEnum(types.TypeDecorator[Optional[_TIntEnum]]): |
|
||
self.enum_type = enum_type | ||
|
||
def process_result_value( # type: ignore[override] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def process_result_value( # type: ignore[override] | |
def process_result_value( |
@@ -1,8 +1,11 @@ | |||
from typing import Any, cast | |||
from enum import IntEnum as _IntEnum |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from enum import IntEnum as _IntEnum | |
import enum |
|
||
from sqlalchemy import types | ||
from sqlalchemy.engine.interfaces import Dialect | ||
|
||
_TIntEnum = TypeVar("_TIntEnum", bound="_IntEnum") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_TIntEnum = TypeVar("_TIntEnum", bound="_IntEnum") | |
_TIntEnum = TypeVar("_TIntEnum", bound=enum.IntEnum) |
super().__init__(*args, **kwargs) | ||
|
||
# validate the input enum type | ||
if not issubclass(enum_type, _IntEnum): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not issubclass(enum_type, _IntEnum): | |
if not issubclass(enum_type, enum.IntEnum): |
I add a IntEnum type decorator which I used in my private project.
I wish it helps!