From 5cee9ecf95be000af8d561cf6016a6b1ddb8d277 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 9 Nov 2022 08:46:46 +0000 Subject: [PATCH 1/4] Improve some DB expression types --- .../contrib/gis/db/models/aggregates.pyi | 14 --- .../contrib/gis/db/models/functions.pyi | 7 -- django-stubs/db/models/expressions.pyi | 104 +++++++++++------- 3 files changed, 62 insertions(+), 63 deletions(-) diff --git a/django-stubs/contrib/gis/db/models/aggregates.pyi b/django-stubs/contrib/gis/db/models/aggregates.pyi index d42cad7bb..ed2adb549 100644 --- a/django-stubs/contrib/gis/db/models/aggregates.pyi +++ b/django-stubs/contrib/gis/db/models/aggregates.pyi @@ -4,22 +4,8 @@ from django.db.models import Aggregate from django.db.models.sql.compiler import _AsSqlType class GeoAggregate(Aggregate): - function: Any is_extent: bool - @property - def output_field(self) -> Any: ... - def as_sql( - self, compiler: Any, connection: Any, function: Any | None = ..., **extra_context: Any - ) -> _AsSqlType: ... def as_oracle(self, compiler: Any, connection: Any, **extra_context: Any) -> Any: ... - def resolve_expression( - self, - query: Any | None = ..., - allow_joins: bool = ..., - reuse: Any | None = ..., - summarize: bool = ..., - for_save: bool = ..., - ) -> Any: ... class Collect(GeoAggregate): name: str diff --git a/django-stubs/contrib/gis/db/models/functions.pyi b/django-stubs/contrib/gis/db/models/functions.pyi index bc5b88d7f..89efe2d60 100644 --- a/django-stubs/contrib/gis/db/models/functions.pyi +++ b/django-stubs/contrib/gis/db/models/functions.pyi @@ -2,20 +2,13 @@ from typing import Any from django.db.models import Func from django.db.models import Transform as StandardTransform -from django.db.models.sql.compiler import _AsSqlType NUMERIC_TYPES: Any class GeoFuncMixin: - function: Any geom_param_pos: Any - def __init__(self, *expressions: Any, **extra: Any) -> None: ... @property def geo_field(self) -> Any: ... - def as_sql( - self, compiler: Any, connection: Any, function: Any | None = ..., **extra_context: Any - ) -> _AsSqlType: ... - def resolve_expression(self, *args: Any, **kwargs: Any) -> Any: ... class GeoFunc(GeoFuncMixin, Func): ... diff --git a/django-stubs/db/models/expressions.pyi b/django-stubs/db/models/expressions.pyi index 8ccfa062f..c2870b1ba 100644 --- a/django-stubs/db/models/expressions.pyi +++ b/django-stubs/db/models/expressions.pyi @@ -115,6 +115,12 @@ class CombinedExpression(SQLiteNumericMixin, Expression): rhs: Combinable def __init__(self, lhs: Combinable, connector: str, rhs: Combinable, output_field: Field | None = ...) -> None: ... +class DurationExpression(CombinedExpression): + def compile(self, side: Combinable, compiler: SQLCompiler, connection: BaseDatabaseWrapper) -> _AsSqlType: ... + +class TemporalSubtraction(CombinedExpression): + def __init__(self, lhs: Combinable, rhs: Combinable) -> None: ... + class F(Combinable): name: str def __init__(self, name: str) -> None: ... @@ -148,30 +154,24 @@ class OuterRef(F): contains_aggregate: bool def relabeled_clone(self: Self, relabels: Any) -> Self: ... -class Subquery(BaseExpression, Combinable): +class Func(SQLiteNumericMixin, Expression): + function: str + name: str template: str - query: Query + arg_joiner: str + arity: int | None + source_expressions: list[Expression] extra: dict[Any, Any] - def __init__(self, queryset: Query | QuerySet, output_field: Field | None = ..., **extra: Any) -> None: ... - -class Exists(Subquery): - negated: bool - def __init__(self, queryset: Query | QuerySet, negated: bool = ..., **kwargs: Any) -> None: ... - def __invert__(self) -> Exists: ... - -class OrderBy(Expression): - template: str - nulls_first: bool - nulls_last: bool - descending: bool - expression: Expression | F | Subquery - def __init__( + def __init__(self, *expressions: Any, output_field: Field | None = ..., **extra: Any) -> None: ... + def as_sql( self, - expression: Expression | F | Subquery, - descending: bool = ..., - nulls_first: bool = ..., - nulls_last: bool = ..., - ) -> None: ... + compiler: SQLCompiler, + connection: BaseDatabaseWrapper, + function: str | None = None, + template: str | None = None, + arg_joiner: str | None = None, + **extra_context: Any, + ) -> _AsSqlType: ... class Value(Expression): value: Any @@ -182,15 +182,25 @@ class RawSQL(Expression): sql: str def __init__(self, sql: str, params: Sequence[Any], output_field: Field | None = ...) -> None: ... -class Func(SQLiteNumericMixin, Expression): - function: str - name: str - template: str - arg_joiner: str - arity: int | None - source_expressions: list[Expression] - extra: dict[Any, Any] - def __init__(self, *expressions: Any, output_field: Field | None = ..., **extra: Any) -> None: ... +class Star(Expression): ... + +class Col(Expression): + target: Field + alias: str + contains_column_references: Literal[True] + possibly_multivalued: Literal[False] + def __init__(self, alias: str, target: Field, output_field: Field | None = ...) -> None: ... + +class Ref(Expression): + def __init__(self, refs: str, source: Expression) -> None: ... + +class ExpressionList(Func): + def __init__(self, *expressions: BaseExpression | Combinable, **extra: Any) -> None: ... + +class OrderByList(Func): ... + +class ExpressionWrapper(Expression): + def __init__(self, expression: Q | Combinable, output_field: Field) -> None: ... class When(Expression): template: str @@ -208,21 +218,31 @@ class Case(Expression): self, *cases: Any, default: Any | None = ..., output_field: Field | None = ..., **extra: Any ) -> None: ... -class ExpressionWrapper(Expression): - def __init__(self, expression: Q | Combinable, output_field: Field) -> None: ... +class Subquery(BaseExpression, Combinable): + template: str + query: Query + extra: dict[Any, Any] + def __init__(self, queryset: Query | QuerySet, output_field: Field | None = ..., **extra: Any) -> None: ... -class Col(Expression): - target: Field - alias: str - contains_column_references: Literal[True] - possibly_multivalued: Literal[False] - def __init__(self, alias: str, target: Field, output_field: Field | None = ...) -> None: ... +class Exists(Subquery): + negated: bool + def __init__(self, queryset: Query | QuerySet, negated: bool = ..., **kwargs: Any) -> None: ... + def __invert__(self) -> Exists: ... -class Ref(Expression): - def __init__(self, refs: str, source: Expression) -> None: ... +class OrderBy(Expression): + template: str + nulls_first: bool + nulls_last: bool + descending: bool + expression: Expression | F | Subquery + def __init__( + self, + expression: Expression | F | Subquery, + descending: bool = ..., + nulls_first: bool = ..., + nulls_last: bool = ..., + ) -> None: ... -class ExpressionList(Func): - def __init__(self, *expressions: BaseExpression | Combinable, **extra: Any) -> None: ... class Window(SQLiteNumericMixin, Expression): template: str From c5060dc68f285a5f49fb432c28a023414d6be8a8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Jan 2023 11:09:37 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks --- django-stubs/db/models/expressions.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/django-stubs/db/models/expressions.pyi b/django-stubs/db/models/expressions.pyi index c2870b1ba..075bd2867 100644 --- a/django-stubs/db/models/expressions.pyi +++ b/django-stubs/db/models/expressions.pyi @@ -243,7 +243,6 @@ class OrderBy(Expression): nulls_last: bool = ..., ) -> None: ... - class Window(SQLiteNumericMixin, Expression): template: str contains_aggregate: bool From 01312e0a8cc119ee7393e3247b0679b63f4cedb6 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sat, 7 Jan 2023 11:12:45 +0000 Subject: [PATCH 3/4] Fix defaults --- django-stubs/db/models/expressions.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/django-stubs/db/models/expressions.pyi b/django-stubs/db/models/expressions.pyi index 075bd2867..b2be3de0f 100644 --- a/django-stubs/db/models/expressions.pyi +++ b/django-stubs/db/models/expressions.pyi @@ -167,9 +167,9 @@ class Func(SQLiteNumericMixin, Expression): self, compiler: SQLCompiler, connection: BaseDatabaseWrapper, - function: str | None = None, - template: str | None = None, - arg_joiner: str | None = None, + function: str | None = ..., + template: str | None = ..., + arg_joiner: str | None = ..., **extra_context: Any, ) -> _AsSqlType: ... From 802c1403d4a581a565584d52cd00081f3dcb0d3d Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Mon, 9 Jan 2023 12:53:36 +0200 Subject: [PATCH 4/4] Cleanup import --- django-stubs/contrib/gis/db/models/aggregates.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/django-stubs/contrib/gis/db/models/aggregates.pyi b/django-stubs/contrib/gis/db/models/aggregates.pyi index ed2adb549..0d1ca38b2 100644 --- a/django-stubs/contrib/gis/db/models/aggregates.pyi +++ b/django-stubs/contrib/gis/db/models/aggregates.pyi @@ -1,7 +1,6 @@ from typing import Any from django.db.models import Aggregate -from django.db.models.sql.compiler import _AsSqlType class GeoAggregate(Aggregate): is_extent: bool