File: client.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 (496 lines) | stat: -rw-r--r-- 16,450 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Callable, Iterable, Iterator, Mapping
from http import HTTPStatus
from http.cookies import SimpleCookie
from io import BytesIO, IOBase
from json import JSONEncoder
from re import Pattern
from types import TracebackType
from typing import Any, Generic, Literal, NoReturn, TypeAlias, TypedDict, TypeVar, type_check_only

from asgiref.typing import ASGIVersions
from django.contrib.auth.models import _User
from django.contrib.sessions.backends.base import SessionBase
from django.core.handlers.asgi import ASGIRequest
from django.core.handlers.base import BaseHandler
from django.core.handlers.wsgi import WSGIRequest
from django.http.request import HttpRequest
from django.http.response import HttpResponseBase
from django.template.base import Template
from django.test.utils import ContextList
from django.urls import ResolverMatch
from django.utils.functional import _StrOrPromise, cached_property

BOUNDARY: str
MULTIPART_CONTENT: str
CONTENT_TYPE_RE: Pattern[str]
JSON_CONTENT_TYPE_RE: Pattern[str]
REDIRECT_STATUS_CODES: frozenset[HTTPStatus]

class RedirectCycleError(Exception):
    last_response: HttpResponseBase
    redirect_chain: list[tuple[str, int]]
    def __init__(self, message: str, last_response: HttpResponseBase) -> None: ...

class FakePayload(IOBase):
    read_started: bool
    def __init__(self, initial_bytes: bytes | str | None = ...) -> None: ...
    def __len__(self) -> int: ...
    def read(self, size: int = ..., /) -> bytes: ...
    def readline(self, size: int | None = ..., /) -> bytes: ...
    def write(self, content: bytes | str, /) -> None: ...

_T = TypeVar("_T")

def closing_iterator_wrapper(iterable: Iterable[_T], close: Callable[[], Any]) -> Iterator[_T]: ...
async def aclosing_iterator_wrapper(iterable: AsyncIterable[_T], close: Callable[[], Any]) -> AsyncIterator[_T]: ...
def conditional_content_removal(request: HttpRequest, response: HttpResponseBase) -> HttpResponseBase: ...
@type_check_only
class _WSGIResponse(HttpResponseBase):
    wsgi_request: WSGIRequest

@type_check_only
class _ASGIResponse(HttpResponseBase):
    asgi_request: ASGIRequest

class ClientHandler(BaseHandler):
    enforce_csrf_checks: bool
    def __init__(self, enforce_csrf_checks: bool = ..., *args: Any, **kwargs: Any) -> None: ...
    def __call__(self, environ: dict[str, Any]) -> _WSGIResponse: ...

class AsyncClientHandler(BaseHandler):
    enforce_csrf_checks: bool
    def __init__(self, enforce_csrf_checks: bool = ..., *args: Any, **kwargs: Any) -> None: ...
    async def __call__(self, scope: dict[str, Any]) -> _ASGIResponse: ...

def encode_multipart(boundary: str, data: dict[str, Any]) -> bytes: ...
def encode_file(boundary: str, key: str, file: Any) -> list[bytes]: ...

_GetDataType: TypeAlias = (
    Mapping[str, str | bytes | int | Iterable[str | bytes | int]]
    | Iterable[tuple[str, str | bytes | int | Iterable[str | bytes | int]]]
    | None
)

@type_check_only
class _RequestFactory(Generic[_T]):
    json_encoder: type[JSONEncoder]
    defaults: dict[str, str]
    cookies: SimpleCookie
    errors: BytesIO
    def __init__(
        self,
        *,
        json_encoder: type[JSONEncoder] = ...,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **defaults: Any,
    ) -> None: ...
    def request(self, **request: Any) -> _T: ...
    def get(
        self,
        path: _StrOrPromise,
        data: _GetDataType = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _T: ...
    def post(
        self,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _T: ...
    def head(
        self,
        path: _StrOrPromise,
        data: Any = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _T: ...
    def trace(
        self,
        path: _StrOrPromise,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _T: ...
    def options(
        self,
        path: _StrOrPromise,
        data: dict[str, str] | str = ...,
        content_type: str = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _T: ...
    def put(
        self,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _T: ...
    def patch(
        self,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _T: ...
    def delete(
        self,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _T: ...
    def generic(
        self,
        method: str,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str | None = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _T: ...

class RequestFactory(_RequestFactory[WSGIRequest]): ...

# A non total duplication of `asgiref.typing.HTTPScope`
@type_check_only
class _HTTPScope(TypedDict, total=False):
    type: Literal["http"]
    asgi: ASGIVersions
    http_version: str
    method: str
    scheme: str
    path: str
    raw_path: bytes
    query_string: bytes
    root_path: str
    headers: Iterable[tuple[bytes, bytes]]
    client: tuple[str, int] | None
    server: tuple[str, int | None] | None
    state: dict[str, Any]
    extensions: dict[str, dict[object, object]] | None

@type_check_only
class _AsyncRequestFactory(_RequestFactory[_T]):
    defaults: _HTTPScope  # type: ignore[assignment]

class AsyncRequestFactory(_AsyncRequestFactory[ASGIRequest]): ...

# fakes to distinguish WSGIRequest and ASGIRequest
@type_check_only
class _MonkeyPatchedWSGIResponse(_WSGIResponse):
    def json(self) -> Any: ...
    request: dict[str, Any]
    client: Client
    templates: list[Template]

    # `context` and `context_data` are populated based on whether you use the standard template
    # backend or a custom one respectively.
    # `context_data` only exists if the response was successful too as the return type changes.
    # `HTTPResponse` when API failed and `TemplateResponse` when successful.
    # https://docs.djangoproject.com/en/stable/topics/testing/tools/#django.test.Response.context
    context: ContextList | dict[str, Any]
    # Must match `django.template.response.SimpleTemplateResponse.context_data`
    context_data: dict[str, Any] | None

    content: bytes
    resolver_match: ResolverMatch
    redirect_chain: list[tuple[str, int]]
    @cached_property
    def text(self) -> str: ...

@type_check_only
class _MonkeyPatchedASGIResponse(_ASGIResponse):
    def json(self) -> Any: ...
    request: dict[str, Any]
    client: AsyncClient
    templates: list[Template]

    # `context` and `context_data` are populated based on whether you use the standard template
    # backend or a custom one respectively.
    # `context_data` only exists if the response was successful too as the return type changes.
    # `HTTPResponse` when API failed and `TemplateResponse` when successful.
    # https://docs.djangoproject.com/en/stable/topics/testing/tools/#django.test.Response.context
    context: ContextList | dict[str, Any]
    # Must match `django.template.response.SimpleTemplateResponse.context_data`
    context_data: dict[str, Any] | None

    content: bytes
    resolver_match: ResolverMatch
    redirect_chain: list[tuple[str, int]]

class ClientMixin:
    def store_exc_info(self, **kwargs: Any) -> None: ...
    def check_exception(self, response: HttpResponseBase) -> NoReturn: ...
    @property
    def session(self) -> SessionBase: ...
    async def asession(self) -> SessionBase: ...
    def login(self, **credentials: Any) -> bool: ...
    async def alogin(self, **credentials: Any) -> bool: ...
    def force_login(self, user: _User, backend: str | None = ...) -> None: ...
    async def aforce_login(self, user: _User, backend: str | None = ...) -> None: ...
    def logout(self) -> None: ...
    async def alogout(self) -> None: ...

class Client(ClientMixin, _RequestFactory[_MonkeyPatchedWSGIResponse]):
    handler: ClientHandler
    raise_request_exception: bool
    exc_info: tuple[type[BaseException], BaseException, TracebackType] | None
    extra: dict[str, Any] | None
    headers: dict[str, Any]
    def __init__(
        self,
        enforce_csrf_checks: bool = ...,
        raise_request_exception: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **defaults: Any,
    ) -> None: ...
    def request(self, **request: Any) -> _MonkeyPatchedWSGIResponse: ...
    def get(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: _GetDataType = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedWSGIResponse: ...
    def post(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedWSGIResponse: ...
    def head(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: Any = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedWSGIResponse: ...
    def options(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: dict[str, str] | str = ...,
        content_type: str = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedWSGIResponse: ...
    def put(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedWSGIResponse: ...
    def patch(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedWSGIResponse: ...
    def delete(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedWSGIResponse: ...
    def trace(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: Any = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedWSGIResponse: ...

class AsyncClient(ClientMixin, _AsyncRequestFactory[Awaitable[_MonkeyPatchedASGIResponse]]):
    handler: AsyncClientHandler
    raise_request_exception: bool
    exc_info: Any
    extra: dict[str, Any] | None
    headers: dict[str, Any]
    def __init__(
        self,
        enforce_csrf_checks: bool = ...,
        raise_request_exception: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **defaults: Any,
    ) -> None: ...
    async def request(self, **request: Any) -> _MonkeyPatchedASGIResponse: ...
    async def get(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: _GetDataType = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedASGIResponse: ...
    async def post(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedASGIResponse: ...
    async def head(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: Any = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedASGIResponse: ...
    async def options(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: dict[str, str] | str = ...,
        content_type: str = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedASGIResponse: ...
    async def put(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedASGIResponse: ...
    async def patch(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedASGIResponse: ...
    async def delete(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: Any = ...,
        content_type: str = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedASGIResponse: ...
    async def trace(  # type: ignore[override]
        self,
        path: _StrOrPromise,
        data: Any = ...,
        follow: bool = ...,
        secure: bool = ...,
        *,
        headers: Mapping[str, Any] | None = ...,
        query_params: Mapping[Any, Any] | None = ...,
        **extra: Any,
    ) -> _MonkeyPatchedASGIResponse: ...

__all__ = (
    "AsyncClient",
    "AsyncRequestFactory",
    "Client",
    "RedirectCycleError",
    "RequestFactory",
    "encode_file",
    "encode_multipart",
)