Skip to content

Commit 4de75b5

Browse files
author
KunxiSun
committed
Merge branch 'feat/IntEnum' of https://github.com/KunxiSun/sqlmodel into feat/IntEnum
2 parents f462b3e + 0f49d7a commit 4de75b5

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

sqlmodel/sql/sqltypes.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def load_dialect_impl(self, dialect: Dialect) -> "types.TypeEngine[Any]":
1919
return dialect.type_descriptor(types.String(self.mysql_default_length))
2020
return super().load_dialect_impl(dialect)
2121

22+
2223
class IntEnum(types.TypeDecorator): # type: ignore
2324
"""TypeDecorator for Integer-enum conversion.
2425
@@ -30,7 +31,7 @@ class IntEnum(types.TypeDecorator): # type: ignore
3031
Example:
3132
>>> class HeroStatus(enum.IntEnum):
3233
... ACTIVE = 1
33-
... DISABLE = 2
34+
... DISABLE = 2
3435
>>>>
3536
>>> from sqlmodel import IntEnum
3637
>>> class Hero(SQLModel):
@@ -51,9 +52,7 @@ def __init__(self, enum_type: _TIntEnum, *args, **kwargs):
5152

5253
# validate the input enum type
5354
if not issubclass(enum_type, _IntEnum):
54-
raise TypeError(
55-
f"Input must be enum.IntEnum"
56-
)
55+
raise TypeError("Input must be enum.IntEnum")
5756

5857
self.enum_type = enum_type
5958

tests/test_enums.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_json_schema_flat_model_pydantic_v1():
7878
"description": "An enumeration.",
7979
"enum": [1, 3],
8080
"type": "int",
81-
}
81+
},
8282
},
8383
}
8484

@@ -106,7 +106,7 @@ def test_json_schema_inherit_model_pydantic_v1():
106106
"description": "An int enumeration.",
107107
"enum": [1, 3],
108108
"type": "int",
109-
}
109+
},
110110
},
111111
}
112112

tests/test_enums_models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import enum
22
import uuid
33

4-
from sqlmodel import Field, SQLModel, IntEnum
4+
from sqlmodel import Field, IntEnum, SQLModel
55

66

77
class MyEnum1(str, enum.Enum):
@@ -13,15 +13,18 @@ class MyEnum2(str, enum.Enum):
1313
C = "C"
1414
D = "D"
1515

16+
1617
class MyEnum3(enum.IntEnum):
1718
E = 1
1819
F = 2
1920

21+
2022
class BaseModel(SQLModel):
2123
id: uuid.UUID = Field(primary_key=True)
2224
enum_field: MyEnum2
2325
int_enum_field: MyEnum3
2426

27+
2528
class FlatModel(SQLModel, table=True):
2629
id: uuid.UUID = Field(primary_key=True)
2730
enum_field: MyEnum1

0 commit comments

Comments
 (0)