From f9589e990a2e612edeabe57d23cb315ab7f67374 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Mon, 1 Jan 2024 19:08:23 +0100 Subject: [PATCH] Add slicing notation to F expressions --- django-stubs/db/models/expressions.pyi | 6 +++++- django-stubs/db/models/fields/__init__.pyi | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/django-stubs/db/models/expressions.pyi b/django-stubs/db/models/expressions.pyi index cbb55ab98..5bc9fd26a 100644 --- a/django-stubs/db/models/expressions.pyi +++ b/django-stubs/db/models/expressions.pyi @@ -130,6 +130,7 @@ class TemporalSubtraction(CombinedExpression): class F(_Deconstructible, Combinable): name: str def __init__(self, name: str) -> None: ... + def __getitem__(self, subscript: int | slice) -> Sliced: ... def resolve_expression( self, query: Any = ..., @@ -152,7 +153,10 @@ class F(_Deconstructible, Combinable): nulls_first: bool | None = ..., nulls_last: bool | None = ..., ) -> OrderBy: ... - def copy(self) -> F: ... + def copy(self) -> Self: ... + +class Sliced(F): + def __init__(self, obj: F, subscript: int | slice) -> None: ... class ResolvedOuterRef(F): contains_aggregate: ClassVar[bool] diff --git a/django-stubs/db/models/fields/__init__.pyi b/django-stubs/db/models/fields/__init__.pyi index b4323a5e3..9672e60a3 100644 --- a/django-stubs/db/models/fields/__init__.pyi +++ b/django-stubs/db/models/fields/__init__.pyi @@ -11,7 +11,7 @@ from django.core.checks import CheckMessage from django.core.exceptions import FieldDoesNotExist as FieldDoesNotExist from django.db.backends.base.base import BaseDatabaseWrapper from django.db.models import Model -from django.db.models.expressions import Col, Combinable +from django.db.models.expressions import Col, Combinable, Expression, Func from django.db.models.fields.reverse_related import ForeignObjectRel from django.db.models.query_utils import Q, RegisterLookupMixin from django.forms import Widget @@ -239,6 +239,7 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]): def get_attname(self) -> str: ... def get_attname_column(self) -> tuple[str, str]: ... def value_to_string(self, obj: Model) -> str: ... + def slice_expression(self, expression: Expression, start: int, end: int | None) -> Func: ... class IntegerField(Field[_ST, _GT]): _pyi_private_set_type: float | int | str | Combinable