Skip to content

Commit a7a1518

Browse files
Improve some DB expression types (#1243)
* Remove several things from `GeoAggregate` and `GeoFuncMixin` where the inherited types are correct * Add missing kwargs `**extra_context` to `Func.as_sql` * Remove `Agggregate.as_sql` since it’s a pass-through to `Func.as_sql` * Reorganize `db/models/expressions.pyi` to follow the same order as upstream * Add missing classes `DurationExpression`, `TemporalSubtraction`, `Star`, and `OrderByList` Spotted the problem with `Aggregate` when adding Mypy to Django-MySQL, which has a custom aggregate class. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a868e74 commit a7a1518

File tree

3 files changed

+62
-65
lines changed

3 files changed

+62
-65
lines changed

django-stubs/contrib/gis/db/models/aggregates.pyi

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
from typing import Any
22

33
from django.db.models import Aggregate
4-
from django.db.models.sql.compiler import _AsSqlType
54

65
class GeoAggregate(Aggregate):
7-
function: Any
86
is_extent: bool
9-
@property
10-
def output_field(self) -> Any: ...
11-
def as_sql(
12-
self, compiler: Any, connection: Any, function: Any | None = ..., **extra_context: Any
13-
) -> _AsSqlType: ...
147
def as_oracle(self, compiler: Any, connection: Any, **extra_context: Any) -> Any: ...
15-
def resolve_expression(
16-
self,
17-
query: Any | None = ...,
18-
allow_joins: bool = ...,
19-
reuse: Any | None = ...,
20-
summarize: bool = ...,
21-
for_save: bool = ...,
22-
) -> Any: ...
238

249
class Collect(GeoAggregate):
2510
name: str

django-stubs/contrib/gis/db/models/functions.pyi

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@ from typing import Any
22

33
from django.db.models import Func
44
from django.db.models import Transform as StandardTransform
5-
from django.db.models.sql.compiler import _AsSqlType
65

76
NUMERIC_TYPES: Any
87

98
class GeoFuncMixin:
10-
function: Any
119
geom_param_pos: Any
12-
def __init__(self, *expressions: Any, **extra: Any) -> None: ...
1310
@property
1411
def geo_field(self) -> Any: ...
15-
def as_sql(
16-
self, compiler: Any, connection: Any, function: Any | None = ..., **extra_context: Any
17-
) -> _AsSqlType: ...
18-
def resolve_expression(self, *args: Any, **kwargs: Any) -> Any: ...
1912

2013
class GeoFunc(GeoFuncMixin, Func): ...
2114

django-stubs/db/models/expressions.pyi

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ class CombinedExpression(SQLiteNumericMixin, Expression):
115115
rhs: Combinable
116116
def __init__(self, lhs: Combinable, connector: str, rhs: Combinable, output_field: Field | None = ...) -> None: ...
117117

118+
class DurationExpression(CombinedExpression):
119+
def compile(self, side: Combinable, compiler: SQLCompiler, connection: BaseDatabaseWrapper) -> _AsSqlType: ...
120+
121+
class TemporalSubtraction(CombinedExpression):
122+
def __init__(self, lhs: Combinable, rhs: Combinable) -> None: ...
123+
118124
class F(Combinable):
119125
name: str
120126
def __init__(self, name: str) -> None: ...
@@ -148,30 +154,24 @@ class OuterRef(F):
148154
contains_aggregate: bool
149155
def relabeled_clone(self: Self, relabels: Any) -> Self: ...
150156

151-
class Subquery(BaseExpression, Combinable):
157+
class Func(SQLiteNumericMixin, Expression):
158+
function: str
159+
name: str
152160
template: str
153-
query: Query
161+
arg_joiner: str
162+
arity: int | None
163+
source_expressions: list[Expression]
154164
extra: dict[Any, Any]
155-
def __init__(self, queryset: Query | QuerySet, output_field: Field | None = ..., **extra: Any) -> None: ...
156-
157-
class Exists(Subquery):
158-
negated: bool
159-
def __init__(self, queryset: Query | QuerySet, negated: bool = ..., **kwargs: Any) -> None: ...
160-
def __invert__(self) -> Exists: ...
161-
162-
class OrderBy(Expression):
163-
template: str
164-
nulls_first: bool
165-
nulls_last: bool
166-
descending: bool
167-
expression: Expression | F | Subquery
168-
def __init__(
165+
def __init__(self, *expressions: Any, output_field: Field | None = ..., **extra: Any) -> None: ...
166+
def as_sql(
169167
self,
170-
expression: Expression | F | Subquery,
171-
descending: bool = ...,
172-
nulls_first: bool = ...,
173-
nulls_last: bool = ...,
174-
) -> None: ...
168+
compiler: SQLCompiler,
169+
connection: BaseDatabaseWrapper,
170+
function: str | None = ...,
171+
template: str | None = ...,
172+
arg_joiner: str | None = ...,
173+
**extra_context: Any,
174+
) -> _AsSqlType: ...
175175

176176
class Value(Expression):
177177
value: Any
@@ -182,15 +182,25 @@ class RawSQL(Expression):
182182
sql: str
183183
def __init__(self, sql: str, params: Sequence[Any], output_field: Field | None = ...) -> None: ...
184184

185-
class Func(SQLiteNumericMixin, Expression):
186-
function: str
187-
name: str
188-
template: str
189-
arg_joiner: str
190-
arity: int | None
191-
source_expressions: list[Expression]
192-
extra: dict[Any, Any]
193-
def __init__(self, *expressions: Any, output_field: Field | None = ..., **extra: Any) -> None: ...
185+
class Star(Expression): ...
186+
187+
class Col(Expression):
188+
target: Field
189+
alias: str
190+
contains_column_references: Literal[True]
191+
possibly_multivalued: Literal[False]
192+
def __init__(self, alias: str, target: Field, output_field: Field | None = ...) -> None: ...
193+
194+
class Ref(Expression):
195+
def __init__(self, refs: str, source: Expression) -> None: ...
196+
197+
class ExpressionList(Func):
198+
def __init__(self, *expressions: BaseExpression | Combinable, **extra: Any) -> None: ...
199+
200+
class OrderByList(Func): ...
201+
202+
class ExpressionWrapper(Expression):
203+
def __init__(self, expression: Q | Combinable, output_field: Field) -> None: ...
194204

195205
class When(Expression):
196206
template: str
@@ -208,21 +218,30 @@ class Case(Expression):
208218
self, *cases: Any, default: Any | None = ..., output_field: Field | None = ..., **extra: Any
209219
) -> None: ...
210220

211-
class ExpressionWrapper(Expression):
212-
def __init__(self, expression: Q | Combinable, output_field: Field) -> None: ...
213-
214-
class Col(Expression):
215-
target: Field
216-
alias: str
217-
contains_column_references: Literal[True]
218-
possibly_multivalued: Literal[False]
219-
def __init__(self, alias: str, target: Field, output_field: Field | None = ...) -> None: ...
221+
class Subquery(BaseExpression, Combinable):
222+
template: str
223+
query: Query
224+
extra: dict[Any, Any]
225+
def __init__(self, queryset: Query | QuerySet, output_field: Field | None = ..., **extra: Any) -> None: ...
220226

221-
class Ref(Expression):
222-
def __init__(self, refs: str, source: Expression) -> None: ...
227+
class Exists(Subquery):
228+
negated: bool
229+
def __init__(self, queryset: Query | QuerySet, negated: bool = ..., **kwargs: Any) -> None: ...
230+
def __invert__(self) -> Exists: ...
223231

224-
class ExpressionList(Func):
225-
def __init__(self, *expressions: BaseExpression | Combinable, **extra: Any) -> None: ...
232+
class OrderBy(Expression):
233+
template: str
234+
nulls_first: bool
235+
nulls_last: bool
236+
descending: bool
237+
expression: Expression | F | Subquery
238+
def __init__(
239+
self,
240+
expression: Expression | F | Subquery,
241+
descending: bool = ...,
242+
nulls_first: bool = ...,
243+
nulls_last: bool = ...,
244+
) -> None: ...
226245

227246
class Window(SQLiteNumericMixin, Expression):
228247
template: str

0 commit comments

Comments
 (0)