|
| 1 | +""" |
| 2 | +requests._types |
| 3 | +~~~~~~~~~~~~~~~ |
| 4 | +
|
| 5 | +This module contains type aliases used internally by the Requests library. |
| 6 | +These types are not part of the public API and must not be relied upon |
| 7 | +by external code. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from collections.abc import Callable, Iterable, Mapping, MutableMapping |
| 13 | +from typing import ( |
| 14 | + TYPE_CHECKING, |
| 15 | + Any, |
| 16 | + Protocol, |
| 17 | + TypeAlias, |
| 18 | + TypeVar, |
| 19 | + runtime_checkable, |
| 20 | +) |
| 21 | + |
| 22 | +_T_co = TypeVar("_T_co", covariant=True) |
| 23 | + |
| 24 | + |
| 25 | +@runtime_checkable |
| 26 | +class SupportsRead(Protocol[_T_co]): |
| 27 | + def read(self, length: int = ..., /) -> _T_co: ... |
| 28 | + |
| 29 | + |
| 30 | +@runtime_checkable |
| 31 | +class SupportsItems(Protocol): |
| 32 | + def items(self) -> Iterable[tuple[Any, Any]]: ... |
| 33 | + |
| 34 | + |
| 35 | +# These are needed at runtime for default_hooks() return type |
| 36 | +HookType: TypeAlias = Callable[["Response"], Any] |
| 37 | +HooksInputType: TypeAlias = Mapping[str, Iterable[HookType] | HookType] |
| 38 | + |
| 39 | + |
| 40 | +def is_prepared(request: PreparedRequest) -> TypeIs[_ValidatedRequest]: |
| 41 | + """Verify a PreparedRequest has been fully prepared.""" |
| 42 | + if TYPE_CHECKING: |
| 43 | + return request.url is not None and request.method is not None |
| 44 | + # noop at runtime to avoid AssertionError |
| 45 | + return True |
| 46 | + |
| 47 | + |
| 48 | +if TYPE_CHECKING: |
| 49 | + from http.cookiejar import CookieJar |
| 50 | + from typing import TypeAlias, TypedDict |
| 51 | + |
| 52 | + from typing_extensions import ( |
| 53 | + Buffer, # TODO: move to collections.abc when Python >= 3.12 |
| 54 | + TypeIs, # TODO: move to typing when Python >= 3.13 |
| 55 | + ) |
| 56 | + |
| 57 | + from .auth import AuthBase |
| 58 | + from .cookies import RequestsCookieJar |
| 59 | + from .models import PreparedRequest, Response |
| 60 | + from .structures import CaseInsensitiveDict |
| 61 | + |
| 62 | + class _ValidatedRequest(PreparedRequest): |
| 63 | + """Subtype asserting a PreparedRequest has been fully prepared before calling. |
| 64 | +
|
| 65 | + The override suppression is required because mutable attribute types are |
| 66 | + invariant (Liskov), but we only narrow after preparation is complete. This |
| 67 | + is the explicit contract for Requests but Python's typing doesn't have a |
| 68 | + better way to represent the requirement. |
| 69 | + """ |
| 70 | + |
| 71 | + url: str # type: ignore[reportIncompatibleVariableOverride] |
| 72 | + method: str # type: ignore[reportIncompatibleVariableOverride] |
| 73 | + |
| 74 | + # Type aliases for core API concepts (ordered by request() signature) |
| 75 | + UriType: TypeAlias = str | bytes |
| 76 | + |
| 77 | + _ParamsMappingKeyType: TypeAlias = str | bytes | int | float |
| 78 | + _ParamsMappingValueType: TypeAlias = ( |
| 79 | + str | bytes | int | float | Iterable[str | bytes | int | float] | None |
| 80 | + ) |
| 81 | + ParamsType: TypeAlias = ( |
| 82 | + Mapping[_ParamsMappingKeyType, _ParamsMappingValueType] |
| 83 | + | tuple[tuple[_ParamsMappingKeyType, _ParamsMappingValueType], ...] |
| 84 | + | Iterable[tuple[_ParamsMappingKeyType, _ParamsMappingValueType]] |
| 85 | + | str |
| 86 | + | bytes |
| 87 | + | None |
| 88 | + ) |
| 89 | + |
| 90 | + KVDataType: TypeAlias = Iterable[tuple[Any, Any]] | Mapping[Any, Any] |
| 91 | + |
| 92 | + RawDataType: TypeAlias = KVDataType | str | bytes |
| 93 | + StreamDataType: TypeAlias = SupportsRead[str | bytes] |
| 94 | + EncodableDataType: TypeAlias = RawDataType | StreamDataType |
| 95 | + |
| 96 | + DataType: TypeAlias = ( |
| 97 | + KVDataType |
| 98 | + | Iterable[bytes | str] |
| 99 | + | str |
| 100 | + | bytes |
| 101 | + | Buffer |
| 102 | + | SupportsRead[str | bytes] |
| 103 | + | None |
| 104 | + ) |
| 105 | + |
| 106 | + BodyType: TypeAlias = ( |
| 107 | + bytes | str | Iterable[bytes | str] | SupportsRead[bytes | str] | None |
| 108 | + ) |
| 109 | + |
| 110 | + HeadersType: TypeAlias = CaseInsensitiveDict[str] | Mapping[str, str | bytes] |
| 111 | + HeadersUpdateType: TypeAlias = Mapping[str, str | bytes | None] |
| 112 | + |
| 113 | + CookiesType: TypeAlias = RequestsCookieJar | Mapping[str, str] |
| 114 | + |
| 115 | + # Building blocks for FilesType |
| 116 | + _FileName: TypeAlias = str | None |
| 117 | + _FileContent: TypeAlias = SupportsRead[str | bytes] | str | bytes |
| 118 | + _FileSpecBasic: TypeAlias = tuple[_FileName, _FileContent] |
| 119 | + _FileSpecWithContentType: TypeAlias = tuple[_FileName, _FileContent, str] |
| 120 | + _FileSpecWithHeaders: TypeAlias = tuple[ |
| 121 | + _FileName, _FileContent, str, CaseInsensitiveDict[str] | Mapping[str, str] |
| 122 | + ] |
| 123 | + _FileSpec: TypeAlias = ( |
| 124 | + _FileContent | _FileSpecBasic | _FileSpecWithContentType | _FileSpecWithHeaders |
| 125 | + ) |
| 126 | + FilesType: TypeAlias = ( |
| 127 | + Mapping[str, _FileSpec] | Iterable[tuple[str, _FileSpec]] | None |
| 128 | + ) |
| 129 | + |
| 130 | + AuthType: TypeAlias = ( |
| 131 | + tuple[str, str] | AuthBase | Callable[[PreparedRequest], PreparedRequest] | None |
| 132 | + ) |
| 133 | + |
| 134 | + TimeoutType: TypeAlias = float | tuple[float | None, float | None] | None |
| 135 | + ProxiesType: TypeAlias = MutableMapping[str, str] |
| 136 | + HooksType: TypeAlias = dict[str, list[HookType]] | None |
| 137 | + VerifyType: TypeAlias = bool | str |
| 138 | + CertType: TypeAlias = str | tuple[str, str] | None |
| 139 | + JsonType: TypeAlias = ( |
| 140 | + None | bool | int | float | str | list["JsonType"] | dict[str, "JsonType"] |
| 141 | + ) |
| 142 | + |
| 143 | + # TypedDicts for Unpack kwargs (PEP 692) |
| 144 | + |
| 145 | + class BaseRequestKwargs(TypedDict, total=False): |
| 146 | + headers: Mapping[str, str | bytes] | None |
| 147 | + cookies: RequestsCookieJar | CookieJar | dict[str, str] | None |
| 148 | + files: FilesType |
| 149 | + auth: AuthType |
| 150 | + timeout: TimeoutType |
| 151 | + allow_redirects: bool |
| 152 | + proxies: dict[str, str] | None |
| 153 | + hooks: HooksType |
| 154 | + stream: bool | None |
| 155 | + verify: VerifyType | None |
| 156 | + cert: CertType |
| 157 | + |
| 158 | + class RequestKwargs(BaseRequestKwargs, total=False): |
| 159 | + """kwargs for request(), options(), head(), delete().""" |
| 160 | + |
| 161 | + params: ParamsType |
| 162 | + data: DataType |
| 163 | + json: JsonType |
| 164 | + |
| 165 | + class GetKwargs(BaseRequestKwargs, total=False): |
| 166 | + data: DataType |
| 167 | + json: JsonType |
| 168 | + |
| 169 | + class PostKwargs(BaseRequestKwargs, total=False): |
| 170 | + params: ParamsType |
| 171 | + |
| 172 | + class DataKwargs(BaseRequestKwargs, total=False): |
| 173 | + """kwargs for put(), patch().""" |
| 174 | + |
| 175 | + params: ParamsType |
| 176 | + json: JsonType |
0 commit comments