From af23ba8bc86cc16931fe88afd5a945e4a34b3ca1 Mon Sep 17 00:00:00 2001 From: diachkow Date: Mon, 21 Apr 2025 10:11:09 +0300 Subject: [PATCH 1/6] =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8F=20Adjust=20type=20ann?= =?UTF-8?q?otation=20for=20Field.sa=5Ftype=20to=20support=20instantiated?= =?UTF-8?q?=20SQLAlchemy=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlmodel/main.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 45a41997fe..fb0de8a96a 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -50,8 +50,9 @@ from sqlalchemy.orm.attributes import set_attribute from sqlalchemy.orm.decl_api import DeclarativeMeta from sqlalchemy.orm.instrumentation import is_instrumented -from sqlalchemy.sql.schema import MetaData +from sqlalchemy.sql.schema import MetaData, SchemaEventTarget from sqlalchemy.sql.sqltypes import LargeBinary, Time, Uuid +from sqlalchemy.types import TypeEngine from typing_extensions import Literal, TypeAlias, deprecated, get_origin from ._compat import ( # type: ignore[attr-defined] @@ -96,6 +97,7 @@ Mapping[int, Union["IncEx", bool]], Mapping[str, Union["IncEx", bool]], ] +SaType: TypeAlias = Union[TypeEngine[Any], Type[TypeEngine[Any]], SchemaEventTarget] OnDeleteType = Literal["CASCADE", "SET NULL", "RESTRICT"] @@ -245,7 +247,7 @@ def Field( unique: Union[bool, UndefinedType] = Undefined, nullable: Union[bool, UndefinedType] = Undefined, index: Union[bool, UndefinedType] = Undefined, - sa_type: Union[Type[Any], UndefinedType] = Undefined, + sa_type: Union[SaType, UndefinedType] = Undefined, sa_column_args: Union[Sequence[Any], UndefinedType] = Undefined, sa_column_kwargs: Union[Mapping[str, Any], UndefinedType] = Undefined, schema_extra: Optional[Dict[str, Any]] = None, @@ -291,7 +293,7 @@ def Field( unique: Union[bool, UndefinedType] = Undefined, nullable: Union[bool, UndefinedType] = Undefined, index: Union[bool, UndefinedType] = Undefined, - sa_type: Union[Type[Any], UndefinedType] = Undefined, + sa_type: Union[SaType, UndefinedType] = Undefined, sa_column_args: Union[Sequence[Any], UndefinedType] = Undefined, sa_column_kwargs: Union[Mapping[str, Any], UndefinedType] = Undefined, schema_extra: Optional[Dict[str, Any]] = None, @@ -380,7 +382,7 @@ def Field( unique: Union[bool, UndefinedType] = Undefined, nullable: Union[bool, UndefinedType] = Undefined, index: Union[bool, UndefinedType] = Undefined, - sa_type: Union[Type[Any], UndefinedType] = Undefined, + sa_type: Union[SaType, UndefinedType] = Undefined, sa_column: Union[Column, UndefinedType] = Undefined, # type: ignore sa_column_args: Union[Sequence[Any], UndefinedType] = Undefined, sa_column_kwargs: Union[Mapping[str, Any], UndefinedType] = Undefined, From e76daeb1b9b24f2d938142152e40100357ddb19f Mon Sep 17 00:00:00 2001 From: Vitalii Diachkov Date: Mon, 1 Sep 2025 08:42:21 +0300 Subject: [PATCH 2/6] feat: rename SaType to SaTypeOrInstance --- sqlmodel/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index decf2e54e1..4f2a286b72 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -97,7 +97,7 @@ Mapping[int, Union["IncEx", bool]], Mapping[str, Union["IncEx", bool]], ] -SaType: TypeAlias = Union[TypeEngine[Any], Type[TypeEngine[Any]], SchemaEventTarget] +SaTypeOrInstance: TypeAlias = Union[TypeEngine[Any], Type[TypeEngine[Any]], SchemaEventTarget] OnDeleteType = Literal["CASCADE", "SET NULL", "RESTRICT"] @@ -247,7 +247,7 @@ def Field( unique: Union[bool, UndefinedType] = Undefined, nullable: Union[bool, UndefinedType] = Undefined, index: Union[bool, UndefinedType] = Undefined, - sa_type: Union[SaType, UndefinedType] = Undefined, + sa_type: Union[SaTypeOrInstance, UndefinedType] = Undefined, sa_column_args: Union[Sequence[Any], UndefinedType] = Undefined, sa_column_kwargs: Union[Mapping[str, Any], UndefinedType] = Undefined, schema_extra: Optional[Dict[str, Any]] = None, @@ -293,7 +293,7 @@ def Field( unique: Union[bool, UndefinedType] = Undefined, nullable: Union[bool, UndefinedType] = Undefined, index: Union[bool, UndefinedType] = Undefined, - sa_type: Union[SaType, UndefinedType] = Undefined, + sa_type: Union[SaTypeOrInstance, UndefinedType] = Undefined, sa_column_args: Union[Sequence[Any], UndefinedType] = Undefined, sa_column_kwargs: Union[Mapping[str, Any], UndefinedType] = Undefined, schema_extra: Optional[Dict[str, Any]] = None, @@ -382,7 +382,7 @@ def Field( unique: Union[bool, UndefinedType] = Undefined, nullable: Union[bool, UndefinedType] = Undefined, index: Union[bool, UndefinedType] = Undefined, - sa_type: Union[SaType, UndefinedType] = Undefined, + sa_type: Union[SaTypeOrInstance, UndefinedType] = Undefined, sa_column: Union[Column, UndefinedType] = Undefined, # type: ignore sa_column_args: Union[Sequence[Any], UndefinedType] = Undefined, sa_column_kwargs: Union[Mapping[str, Any], UndefinedType] = Undefined, From 96199fae6344b7ae76e4fa8f5cc47a2a0123ca13 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 1 Sep 2025 05:42:28 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlmodel/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 4f2a286b72..b128e20912 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -97,7 +97,9 @@ Mapping[int, Union["IncEx", bool]], Mapping[str, Union["IncEx", bool]], ] -SaTypeOrInstance: TypeAlias = Union[TypeEngine[Any], Type[TypeEngine[Any]], SchemaEventTarget] +SaTypeOrInstance: TypeAlias = Union[ + TypeEngine[Any], Type[TypeEngine[Any]], SchemaEventTarget +] OnDeleteType = Literal["CASCADE", "SET NULL", "RESTRICT"] From 8b629d6584eb40332904a24b639645b24d37e176 Mon Sep 17 00:00:00 2001 From: Vitalii Diachkov Date: Mon, 1 Sep 2025 09:24:51 +0300 Subject: [PATCH 4/6] apply formatting --- sqlmodel/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index b128e20912..9162e482ac 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -98,7 +98,9 @@ Mapping[str, Union["IncEx", bool]], ] SaTypeOrInstance: TypeAlias = Union[ - TypeEngine[Any], Type[TypeEngine[Any]], SchemaEventTarget + TypeEngine[Any], + Type[TypeEngine[Any]], + SchemaEventTarget, ] OnDeleteType = Literal["CASCADE", "SET NULL", "RESTRICT"] From 6c95e05fd74a2e0aa0b64ff1178bf0a0e48ac832 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Tue, 3 Mar 2026 20:14:28 +0000 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=8E=A8=20Auto=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlmodel/main.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 476c33daee..022dfa3730 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -89,11 +89,9 @@ | Mapping[int, Union["IncEx", bool]] | Mapping[str, Union["IncEx", bool]] ) -SaTypeOrInstance: TypeAlias = Union[ - TypeEngine[Any], - type[TypeEngine[Any]], - SchemaEventTarget, -] +SaTypeOrInstance: TypeAlias = ( + TypeEngine[Any] | type[TypeEngine[Any]] | SchemaEventTarget +) OnDeleteType = Literal["CASCADE", "SET NULL", "RESTRICT"] From afbed477947897a659f5f85f38e1b3ec6dacd437 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Tue, 3 Mar 2026 21:24:09 +0100 Subject: [PATCH 6/6] also fix the type in the dataclass --- sqlmodel/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 022dfa3730..86f62dd5ff 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -213,7 +213,7 @@ class FieldInfoMetadata: ondelete: OnDeleteType | UndefinedType = Undefined unique: bool | UndefinedType = Undefined index: bool | UndefinedType = Undefined - sa_type: type[Any] | UndefinedType = Undefined + sa_type: SaTypeOrInstance | UndefinedType = Undefined sa_column: Column[Any] | UndefinedType = Undefined sa_column_args: Sequence[Any] | UndefinedType = Undefined sa_column_kwargs: Mapping[str, Any] | UndefinedType = Undefined