Skip to content

Commit 3d04d94

Browse files
KunxiSunYuriiMotov
andauthored
Apply suggestions from code review
Co-authored-by: Motov Yurii <[email protected]>
1 parent 412fed1 commit 3d04d94

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

sqlmodel/sql/sqltypes.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from enum import IntEnum as _IntEnum
1+
import enum
22
from typing import Any, Optional, Type, TypeVar, cast
33

44
from sqlalchemy import types
55
from sqlalchemy.engine.interfaces import Dialect
66

7-
_TIntEnum = TypeVar("_TIntEnum", bound="_IntEnum")
7+
_TIntEnum = TypeVar("_TIntEnum", bound=enum.IntEnum)
88

99

1010
class AutoString(types.TypeDecorator): # type: ignore
@@ -19,37 +19,29 @@ def load_dialect_impl(self, dialect: Dialect) -> "types.TypeEngine[Any]":
1919
return super().load_dialect_impl(dialect)
2020

2121

22-
class IntEnum(types.TypeDecorator): # type: ignore
22+
class IntEnum(types.TypeDecorator[Optional[_TIntEnum]]):
2323
impl = types.Integer
2424
cache_ok = True
2525

2626
def __init__(self, enum_type: Type[_TIntEnum], *args: Any, **kwargs: Any):
2727
super().__init__(*args, **kwargs)
2828

2929
# validate the input enum type
30-
if not issubclass(enum_type, _IntEnum):
30+
if not issubclass(enum_type, enum.IntEnum):
3131
raise TypeError("Input must be enum.IntEnum")
3232

3333
self.enum_type = enum_type
3434

35-
def process_result_value( # type: ignore[override]
35+
def process_result_value(
3636
self,
3737
value: Optional[int],
3838
dialect: Dialect,
3939
) -> Optional[_TIntEnum]:
40-
if value is None:
41-
return None
42-
43-
result = self.enum_type(value)
44-
return result
40+
return None if (value is None) else self.enum_type(value)
4541

4642
def process_bind_param(
4743
self,
4844
value: Optional[_TIntEnum],
4945
dialect: Dialect,
5046
) -> Optional[int]:
51-
if value is None:
52-
return None
53-
54-
result = value.value
55-
return result
47+
return None if (value is None) else value.value

0 commit comments

Comments
 (0)