File: query.pyi

package info (click to toggle)
python-django-stubs 5.2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,832 kB
  • sloc: python: 5,185; makefile: 15; sh: 8
file content (293 lines) | stat: -rw-r--r-- 13,202 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import datetime
from collections.abc import AsyncIterator, Collection, Iterable, Iterator, Mapping, Sequence, Sized
from typing import Any, Generic, Literal, NamedTuple, TypeAlias, overload

from django.db.backends.utils import _ExecuteQuery
from django.db.models import Manager
from django.db.models.base import Model
from django.db.models.expressions import Combinable, OrderBy
from django.db.models.sql.query import Query, RawQuery
from django.utils.functional import cached_property
from typing_extensions import Self, TypeVar, deprecated

_T = TypeVar("_T", covariant=True)
_Model = TypeVar("_Model", bound=Model, covariant=True)
_Row = TypeVar("_Row", covariant=True, default=_Model)  # ONLY use together with _Model
_TupleT = TypeVar("_TupleT", bound=tuple[Any, ...], covariant=True)

# The type of the lookup passed to Prefetch(...)
# This will be specialized to a `LiteralString` in the plugin for further processing and validation
_LookupT = TypeVar("_LookupT", bound=str, covariant=True)
# The type of the queryset passed to Prefetch(...)
_PrefetchedQuerySetT = TypeVar("_PrefetchedQuerySetT", bound=QuerySet[Model], covariant=True, default=QuerySet[Model])
# The attribute name to store the prefetched list[_PrefetchedQuerySetT]
# This will be specialized to a `LiteralString` in the plugin for further processing and validation
_ToAttrT = TypeVar("_ToAttrT", bound=str, covariant=True, default=str)

_OrderByFieldName: TypeAlias = str | Combinable

MAX_GET_RESULTS: int
REPR_OUTPUT_SIZE: int
PROHIBITED_FILTER_KWARGS: frozenset[str]

class BaseIterable(Generic[_T]):
    queryset: QuerySet[Model]
    chunked_fetch: bool
    chunk_size: int
    def __init__(self, queryset: QuerySet[Model], chunked_fetch: bool = False, chunk_size: int = 100) -> None: ...
    def __aiter__(self) -> AsyncIterator[_T]: ...

class ModelIterable(BaseIterable[_Model], Generic[_Model]):
    def __iter__(self) -> Iterator[_Model]: ...

class RawModelIterable(BaseIterable[dict[str, Any]]):
    def __iter__(self) -> Iterator[dict[str, Any]]: ...

class ValuesIterable(BaseIterable[dict[str, Any]]):
    def __iter__(self) -> Iterator[dict[str, Any]]: ...

class ValuesListIterable(BaseIterable[_TupleT]):
    def __iter__(self) -> Iterator[_TupleT]: ...

class NamedValuesListIterable(ValuesListIterable[NamedTuple]):
    def __iter__(self) -> Iterator[NamedTuple]: ...

class FlatValuesListIterable(BaseIterable[_T]):
    def __iter__(self) -> Iterator[_T]: ...

class QuerySet(Iterable[_Row], Sized, Generic[_Model, _Row]):
    model: type[_Model]
    query: Query
    _iterable_class: type[BaseIterable]
    _result_cache: list[_Row] | None
    def __init__(
        self,
        model: type[Model] | None = None,
        query: Query | None = None,
        using: str | None = None,
        hints: dict[str, Model] | None = None,
    ) -> None: ...
    @classmethod
    def as_manager(cls) -> Manager[_Model]: ...
    def __len__(self) -> int: ...
    def __bool__(self) -> bool: ...
    def __class_getitem__(cls, item: type[_Model]) -> Self: ...
    def __getstate__(self) -> dict[str, Any]: ...
    # Technically, the other QuerySet must be of the same type _T, but _T is covariant
    def __and__(self, other: QuerySet[_Model, _Row]) -> Self: ...
    def __or__(self, other: QuerySet[_Model, _Row]) -> Self: ...
    # IMPORTANT: When updating any of the following methods' signatures, please ALSO modify
    # the corresponding method in BaseManager.
    def iterator(self, chunk_size: int | None = None) -> Iterator[_Row]: ...
    def aiterator(self, chunk_size: int = 2000) -> AsyncIterator[_Row]: ...
    def aggregate(self, *args: Any, **kwargs: Any) -> dict[str, Any]: ...
    async def aaggregate(self, *args: Any, **kwargs: Any) -> dict[str, Any]: ...
    def get(self, *args: Any, **kwargs: Any) -> _Row: ...
    async def aget(self, *args: Any, **kwargs: Any) -> _Row: ...
    def create(self, **kwargs: Any) -> _Model: ...
    async def acreate(self, **kwargs: Any) -> _Model: ...
    def bulk_create(
        self,
        objs: Iterable[_Model],
        batch_size: int | None = None,
        ignore_conflicts: bool = False,
        update_conflicts: bool = False,
        update_fields: Collection[str] | None = None,
        unique_fields: Collection[str] | None = None,
    ) -> list[_Model]: ...
    async def abulk_create(
        self,
        objs: Iterable[_Model],
        batch_size: int | None = None,
        ignore_conflicts: bool = False,
        update_conflicts: bool = False,
        update_fields: Collection[str] | None = None,
        unique_fields: Collection[str] | None = None,
    ) -> list[_Model]: ...
    def bulk_update(self, objs: Iterable[_Model], fields: Iterable[str], batch_size: int | None = None) -> int: ...
    async def abulk_update(
        self, objs: Iterable[_Model], fields: Iterable[str], batch_size: int | None = None
    ) -> int: ...
    def get_or_create(self, defaults: Mapping[str, Any] | None = None, **kwargs: Any) -> tuple[_Model, bool]: ...
    async def aget_or_create(self, defaults: Mapping[str, Any] | None = None, **kwargs: Any) -> tuple[_Model, bool]: ...
    def update_or_create(
        self,
        defaults: Mapping[str, Any] | None = None,
        create_defaults: Mapping[str, Any] | None = None,
        **kwargs: Any,
    ) -> tuple[_Model, bool]: ...
    async def aupdate_or_create(
        self,
        defaults: Mapping[str, Any] | None = None,
        create_defaults: Mapping[str, Any] | None = None,
        **kwargs: Any,
    ) -> tuple[_Model, bool]: ...
    def earliest(self, *fields: str | OrderBy) -> _Row: ...
    async def aearliest(self, *fields: str | OrderBy) -> _Row: ...
    def latest(self, *fields: str | OrderBy) -> _Row: ...
    async def alatest(self, *fields: str | OrderBy) -> _Row: ...
    def first(self) -> _Row | None: ...
    async def afirst(self) -> _Row | None: ...
    def last(self) -> _Row | None: ...
    async def alast(self) -> _Row | None: ...
    def in_bulk(self, id_list: Iterable[Any] | None = None, *, field_name: str = "pk") -> dict[Any, _Model]: ...
    async def ain_bulk(self, id_list: Iterable[Any] | None = None, *, field_name: str = "pk") -> dict[Any, _Model]: ...
    def delete(self) -> tuple[int, dict[str, int]]: ...
    async def adelete(self) -> tuple[int, dict[str, int]]: ...
    def _raw_delete(self, using: str | None) -> int: ...
    def update(self, **kwargs: Any) -> int: ...
    async def aupdate(self, **kwargs: Any) -> int: ...
    def exists(self) -> bool: ...
    async def aexists(self) -> bool: ...
    def explain(self, *, format: str | None = None, **options: Any) -> str: ...
    async def aexplain(self, *, format: str | None = None, **options: Any) -> str: ...
    def contains(self, obj: Model) -> bool: ...
    async def acontains(self, obj: Model) -> bool: ...
    def raw(
        self,
        raw_query: _ExecuteQuery,
        params: Any = (),
        translations: dict[str, str] | None = None,
        using: str | None = None,
    ) -> RawQuerySet[_Model]: ...
    # The type of values may be overridden to be more specific in the mypy plugin, depending on the fields param
    def values(self, *fields: str | Combinable, **expressions: Any) -> QuerySet[_Model, dict[str, Any]]: ...
    # The type of values_list may be overridden to be more specific in the mypy plugin, depending on the fields param
    def values_list(
        self, *fields: str | Combinable, flat: bool = False, named: bool = False
    ) -> QuerySet[_Model, Any]: ...
    def dates(
        self,
        field_name: str,
        kind: Literal["year", "month", "week", "day"],
        order: Literal["ASC", "DESC"] = "ASC",
    ) -> QuerySet[_Model, datetime.date]: ...
    def datetimes(
        self,
        field_name: str,
        kind: Literal["year", "month", "week", "day", "hour", "minute", "second"],
        order: Literal["ASC", "DESC"] = "ASC",
        tzinfo: datetime.tzinfo | None = None,
    ) -> QuerySet[_Model, datetime.datetime]: ...
    def none(self) -> Self: ...
    def all(self) -> Self: ...
    def filter(self, *args: Any, **kwargs: Any) -> Self: ...
    def exclude(self, *args: Any, **kwargs: Any) -> Self: ...
    def complex_filter(self, filter_obj: Any) -> Self: ...
    def count(self) -> int: ...
    async def acount(self) -> int: ...
    def union(self, *other_qs: QuerySet[Model, Any], all: bool = False) -> Self: ...
    def intersection(self, *other_qs: QuerySet[Model, Any]) -> Self: ...
    def difference(self, *other_qs: QuerySet[Model, Any]) -> Self: ...
    def select_for_update(
        self, nowait: bool = False, skip_locked: bool = False, of: Sequence[str] = (), no_key: bool = False
    ) -> Self: ...
    @overload
    def select_related(self, clear: None, /) -> Self: ...
    @overload
    def select_related(self, *fields: str) -> Self: ...
    @overload
    def prefetch_related(self, clear: None, /) -> Self: ...
    @overload
    def prefetch_related(self, *lookups: str | Prefetch[_LookupT, _PrefetchedQuerySetT, _ToAttrT]) -> Self: ...
    def annotate(self, *args: Any, **kwargs: Any) -> Self: ...
    def alias(self, *args: Any, **kwargs: Any) -> Self: ...
    def order_by(self, *field_names: _OrderByFieldName) -> Self: ...
    def distinct(self, *field_names: str) -> Self: ...
    # extra() return type won't be supported any time soon
    def extra(
        self,
        select: dict[str, Any] | None = None,
        where: Sequence[str] | None = None,
        params: Sequence[Any] | None = None,
        tables: Sequence[str] | None = None,
        order_by: Sequence[_OrderByFieldName] | None = None,
        select_params: Sequence[Any] | None = None,
    ) -> QuerySet[Any, Any]: ...
    def reverse(self) -> Self: ...
    @overload
    def defer(self, clear: None, /) -> Self: ...
    @overload
    def defer(self, *fields: str) -> Self: ...
    def only(self, *fields: str) -> Self: ...
    def using(self, alias: str | None) -> Self: ...
    @property
    def ordered(self) -> bool: ...
    @property
    def db(self) -> str: ...
    def _fetch_all(self) -> None: ...
    def resolve_expression(self, *args: Any, **kwargs: Any) -> Any: ...
    def __iter__(self) -> Iterator[_Row]: ...
    def __aiter__(self) -> AsyncIterator[_Row]: ...
    @overload
    def __getitem__(self, i: int) -> _Row: ...
    @overload
    def __getitem__(self, s: slice) -> Self: ...

class RawQuerySet(Iterable[_Model], Sized):
    raw_query: RawQuery | str
    model: type[_Model] | None
    query: RawQuery
    params: tuple[Any, ...] | None
    translations: dict[str, str]

    def __init__(
        self,
        raw_query: RawQuery | str,
        model: type[_Model] | None = None,
        query: Query | None = None,
        params: tuple[Any, ...] = (),
        translations: dict[str, str] | None = None,
        using: str | None = None,
        hints: dict[str, Model] | None = None,
    ) -> None: ...
    def __len__(self) -> int: ...
    def __iter__(self) -> Iterator[_Model]: ...
    def __bool__(self) -> bool: ...
    @overload
    def __getitem__(self, k: int) -> _Model: ...
    @overload
    def __getitem__(self, k: str) -> Any: ...
    @overload
    def __getitem__(self, k: slice) -> Self: ...
    @cached_property
    def columns(self) -> list[str]: ...
    @property
    def db(self) -> str: ...
    def iterator(self) -> Iterator[_Model]: ...
    @cached_property
    def model_fields(self) -> dict[str, str]: ...
    @overload
    def prefetch_related(self, clear: None, /) -> Self: ...
    @overload
    def prefetch_related(self, *lookups: str | Prefetch[_LookupT, _PrefetchedQuerySetT, _ToAttrT]) -> Self: ...
    def resolve_model_init_order(self) -> tuple[list[str], list[int], list[tuple[str, int]]]: ...
    def using(self, alias: str | None) -> Self: ...

# Deprecated alias of QuerySet, for compatibility only.
_QuerySet: TypeAlias = QuerySet  # noqa: PYI047

class Prefetch(Generic[_LookupT, _PrefetchedQuerySetT, _ToAttrT]):
    prefetch_through: str
    prefetch_to: str
    queryset: _PrefetchedQuerySetT | None
    to_attr: _ToAttrT | None
    def __init__(
        self, lookup: _LookupT, queryset: _PrefetchedQuerySetT | None = None, to_attr: _ToAttrT | None = None
    ) -> None: ...
    def __getstate__(self) -> dict[str, Any]: ...
    def add_prefix(self, prefix: str) -> None: ...
    def get_current_prefetch_to(self, level: int) -> str: ...
    def get_current_to_attr(self, level: int) -> tuple[str, str]: ...
    @deprecated(
        "get_current_queryset() is deprecated and will be removed in Django 6.0. Use get_current_querysets() instead."
    )
    def get_current_queryset(self, level: int) -> _PrefetchedQuerySetT | None: ...
    def get_current_querysets(self, level: int) -> list[_PrefetchedQuerySetT] | None: ...

def prefetch_related_objects(model_instances: Sequence[_Model], *related_lookups: str | Prefetch) -> None: ...
async def aprefetch_related_objects(model_instances: Sequence[_Model], *related_lookups: str | Prefetch) -> None: ...
def get_prefetcher(instance: Model, through_attr: str, to_attr: str) -> tuple[Any, Any, bool, bool]: ...

class InstanceCheckMeta(type): ...
class EmptyQuerySet(metaclass=InstanceCheckMeta): ...