Skip to content

Commit b426ac8

Browse files
authored
don't use typing with ServerDefaultType (#180)
The server default is any expression that is passed as DDL to the database and these usually don't have SQL types explicitly stated. With the code as is, pylance is complaining about this: Column(DateTime(), server_default=func.now()) and requiring I do this: Column(DateTime(), server_default=func.now(type_=DateTime)) people don't need to do that, server_defaults SQL type always comes from the column type and doesn't normally need to be stated. Also, column_server_default.py was importing "functions as func", which is wrong. "func" is not a module it's a namespace object, fixed that.
1 parent 195fc64 commit b426ac8

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

sqlalchemy-stubs/sql/schema.pyi

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ _IDX = TypeVar("_IDX", bound=Index)
5454
_CP = TypeVar("_CP", bound=Computed)
5555
_ID = TypeVar("_ID", bound=Identity)
5656

57-
_ServerDefaultType = Union[FetchedValue, str, TextClause, ColumnElement[_T]]
57+
_ServerDefaultType = Union[FetchedValue, str, TextClause, ColumnElement]
5858

5959
class SchemaItem(SchemaEventTarget, visitors.Visitable):
6060
__visit_name__: str = ...
@@ -142,7 +142,7 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_TE]):
142142
primary_key: bool = ...
143143
nullable: bool = ...
144144
default: Optional[Any] = ...
145-
server_default: Optional[_ServerDefaultType[_TE]] = ...
145+
server_default: Optional[_ServerDefaultType] = ...
146146
server_onupdate: Optional[FetchedValue] = ...
147147
index: Optional[bool] = ...
148148
unique: Optional[bool] = ...
@@ -169,7 +169,7 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_TE]):
169169
nullable: bool = ...,
170170
onupdate: Optional[Any] = ...,
171171
primary_key: bool = ...,
172-
server_default: Optional[_ServerDefaultType[Any]] = ...,
172+
server_default: Optional[_ServerDefaultType] = ...,
173173
server_onupdate: Optional[FetchedValue] = ...,
174174
quote: Optional[bool] = ...,
175175
unique: Optional[bool] = ...,
@@ -190,7 +190,7 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_TE]):
190190
nullable: bool = ...,
191191
onupdate: Optional[Any] = ...,
192192
primary_key: bool = ...,
193-
server_default: Optional[_ServerDefaultType[Any]] = ...,
193+
server_default: Optional[_ServerDefaultType] = ...,
194194
server_onupdate: Optional[FetchedValue] = ...,
195195
quote: Optional[bool] = ...,
196196
unique: Optional[bool] = ...,
@@ -213,7 +213,7 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_TE]):
213213
nullable: bool = ...,
214214
onupdate: Optional[Any] = ...,
215215
primary_key: bool = ...,
216-
server_default: Optional[_ServerDefaultType[_TE]] = ...,
216+
server_default: Optional[_ServerDefaultType] = ...,
217217
server_onupdate: Optional[FetchedValue] = ...,
218218
quote: Optional[bool] = ...,
219219
unique: Optional[bool] = ...,
@@ -235,7 +235,7 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_TE]):
235235
nullable: bool = ...,
236236
onupdate: Optional[Any] = ...,
237237
primary_key: bool = ...,
238-
server_default: Optional[_ServerDefaultType[_TE]] = ...,
238+
server_default: Optional[_ServerDefaultType] = ...,
239239
server_onupdate: Optional[FetchedValue] = ...,
240240
quote: Optional[bool] = ...,
241241
unique: Optional[bool] = ...,

test/files/column_server_default.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
from sqlalchemy import Column
33
from sqlalchemy import DateTime
44
from sqlalchemy import FetchedValue
5+
from sqlalchemy import func
56
from sqlalchemy import Integer
67
from sqlalchemy import literal_column
78
from sqlalchemy import text
89
from sqlalchemy import true
910
from sqlalchemy.orm import registry
10-
from sqlalchemy.sql import functions as func
1111

1212
reg: registry = registry()
1313

@@ -19,8 +19,16 @@ class A:
1919
b = Column(Boolean, nullable=False, server_default=true())
2020
c = Column(DateTime, server_default=func.now(), nullable=False)
2121

22-
# EXPECTED_MYPY: Cannot infer type argument 1 of "Column"
23-
d = Column(Boolean, server_default=func.now(), nullable=False)
22+
# this is fine as we don't know anything about func.xyzq() (this was
23+
# previously func.now(), but that's also untyped). The column type
24+
# determines the type.
25+
d = Column(Boolean, server_default=func.xyzq(), nullable=False)
26+
27+
# what would be *nice* to emit an error would be this, but this
28+
# is really not important, people don't usually put types in functions
29+
# as they are usually part of a bigger context where the type is known
30+
# d = Column(Boolean, server_default=func.xyzq(type_=DateTime),
31+
# nullable=False)
2432

2533
e = Column(DateTime, server_default="now()")
2634
f = Column(DateTime, server_default=text("now()"))

0 commit comments

Comments
 (0)