Skip to content

Commit 5981639

Browse files
authored
Allow FunctionElement for Column server_onupdate (#152)
Use common type aliases for Column server_default and server_onupdate
1 parent eabeb82 commit 5981639

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

sqlalchemy-stubs/sql/schema.pyi

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ from .elements import ClauseElement
2929
from .elements import ColumnClause
3030
from .elements import ColumnElement
3131
from .elements import TextClause
32+
from .functions import FunctionElement
3233
from .events import DDLEvents
3334
from .selectable import TableClause
3435
from .. import util
@@ -54,6 +55,9 @@ _IDX = TypeVar("_IDX", bound=Index)
5455
_CP = TypeVar("_CP", bound=Computed)
5556
_ID = TypeVar("_ID", bound=Identity)
5657

58+
_ServerDefaultType = Union[FetchedValue, str, TextClause, ColumnElement[_T]]
59+
_ServerOnUpdateType = Union[FetchedValue, FunctionElement]
60+
5761
class SchemaItem(SchemaEventTarget, visitors.Visitable):
5862
__visit_name__: str = ...
5963
create_drop_stringify_dialect: str = ...
@@ -140,10 +144,8 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_TE]):
140144
primary_key: bool = ...
141145
nullable: bool = ...
142146
default: Optional[Any] = ...
143-
server_default: Optional[
144-
Union[FetchedValue, str, TextClause, ColumnElement[_TE]]
145-
] = ...
146-
server_onupdate: Optional[FetchedValue] = ...
147+
server_default: Optional[_ServerDefaultType[_TE]] = ...
148+
server_onupdate: Optional[_ServerOnUpdateType] = ...
147149
index: Optional[bool] = ...
148150
unique: Optional[bool] = ...
149151
system: bool = ...
@@ -169,10 +171,8 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_TE]):
169171
nullable: bool = ...,
170172
onupdate: Optional[Any] = ...,
171173
primary_key: bool = ...,
172-
server_default: Optional[
173-
Union[FetchedValue, str, TextClause, ColumnElement[Any]]
174-
] = ...,
175-
server_onupdate: Optional[FetchedValue] = ...,
174+
server_default: Optional[_ServerDefaultType[Any]] = ...,
175+
server_onupdate: Optional[_ServerOnUpdateType] = ...,
176176
quote: Optional[bool] = ...,
177177
unique: Optional[bool] = ...,
178178
system: bool = ...,
@@ -192,10 +192,8 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_TE]):
192192
nullable: bool = ...,
193193
onupdate: Optional[Any] = ...,
194194
primary_key: bool = ...,
195-
server_default: Optional[
196-
Union[FetchedValue, str, TextClause, ColumnElement[Any]]
197-
] = ...,
198-
server_onupdate: Optional[FetchedValue] = ...,
195+
server_default: Optional[_ServerDefaultType[Any]] = ...,
196+
server_onupdate: Optional[_ServerOnUpdateType] = ...,
199197
quote: Optional[bool] = ...,
200198
unique: Optional[bool] = ...,
201199
system: bool = ...,
@@ -217,10 +215,8 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_TE]):
217215
nullable: bool = ...,
218216
onupdate: Optional[Any] = ...,
219217
primary_key: bool = ...,
220-
server_default: Optional[
221-
Union[FetchedValue, str, TextClause, ColumnElement[_TE]]
222-
] = ...,
223-
server_onupdate: Optional[FetchedValue] = ...,
218+
server_default: Optional[_ServerDefaultType[_TE]] = ...,
219+
server_onupdate: Optional[_ServerOnUpdateType] = ...,
224220
quote: Optional[bool] = ...,
225221
unique: Optional[bool] = ...,
226222
system: bool = ...,
@@ -241,10 +237,8 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_TE]):
241237
nullable: bool = ...,
242238
onupdate: Optional[Any] = ...,
243239
primary_key: bool = ...,
244-
server_default: Optional[
245-
Union[FetchedValue, str, TextClause, ColumnElement[_TE]]
246-
] = ...,
247-
server_onupdate: Optional[FetchedValue] = ...,
240+
server_default: Optional[_ServerDefaultType[_TE]] = ...,
241+
server_onupdate: Optional[_ServerOnUpdateType] = ...,
248242
quote: Optional[bool] = ...,
249243
unique: Optional[bool] = ...,
250244
system: bool = ...,

test/files/column_server_onupdate.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from sqlalchemy import Column
2+
from sqlalchemy.orm import registry
3+
from sqlalchemy.sql import functions as func
4+
from sqlalchemy.types import Integer
5+
6+
reg: registry = registry()
7+
8+
9+
@reg.mapped
10+
class ColumnServerOnUpdateArgument:
11+
__tablename__ = "onupdate"
12+
13+
# overload 1: (__name: str, *args, ...)
14+
overload1: int = Column("name", server_onupdate=func.now(), nullable=False)
15+
# overload 2: (*args, ...)
16+
Column(server_onupdate=func.now(), nullable=False)
17+
# overload 3: (__name: str, __type: _TE, *args, ...)
18+
overload3 = Column("name", Integer, server_onupdate=func.now(), nullable=False)
19+
# overload 4: (__type: _TE, *args, ...)
20+
overload4 = Column(Integer, server_onupdate=func.now(), nullable=False)

0 commit comments

Comments
 (0)