Switch to `USE_CLIENT_DEFAULT` instead of `UNSET`. (#1634)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Add failing test case for 'content=io.BytesIO(...)' * Refactor peek_filelike_length to return an Optional[int] * Peek filelength on file-like objects when rendering 'content=...' * Switch to USE_CLIENT_DEFAULT * Switch to USE_CLIENT_DEFAULT * Linting
authored by Tom Christie · committed by GitHub · parent 7025dd1 · Browse files · 2 files·+79 added−47 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+79 added−47 removed
- other:+79 added−47 removed
Provenance
- Commit
69409bb- Tree
ca9625b- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
7025dd1
2 files changed+79 added−47 removed
modified:httpx/__init__.py+2 added-1 removed
1 from .__version__ import __description__, __title__, __version__2 from ._api import delete, get, head, options, patch, post, put, request, stream3 from ._auth import Auth, BasicAuth, DigestAuth4-from ._client import AsyncClient, Client4+from ._client import USE_CLIENT_DEFAULT, AsyncClient, Client5 from ._config import Limits, Proxy, Timeout, create_ssl_context6 from ._content import ByteStream7 from ._exceptions import (8 CloseError,101 unmodified lines110 "TooManyRedirects",111 "TransportError",112 "UnsupportedProtocol",113 "URL",114+ "USE_CLIENT_DEFAULT",115 "WriteError",116 "WriteTimeout",117 "WSGITransport",118 ]6 unmodified linesmodified:httpx/_client.py+77 added-46 removed
10 unmodified lines11 from ._config import (12 DEFAULT_LIMITS,13 DEFAULT_MAX_REDIRECTS,14 DEFAULT_TIMEOUT_CONFIG,15- UNSET,15 Limits,16 Proxy,17 Timeout,19- UnsetType,18 )19 from ._decoders import SUPPORTED_DECODERS20 from ._exceptions import (21 InvalidURL,40 unmodified lines62 T = typing.TypeVar("T", bound="Client")63 U = typing.TypeVar("U", bound="AsyncClient")64 65 66+class UseClientDefault:67+ """68+ For some parameters such as `auth=...` and `timeout=...` we need to be able69+ to indicate the default "unset" state, in a way that is distinctly different70+ to using `None`.71+72+ The default "unset" state indicates that whatever default is set on the73+ client should be used. This is different to setting `None`, which74+ explicitly disables the parameter, possibly overriding a client default.75+76+ For example we use `timeout=USE_CLIENT_DEFAULT` in the `request()` signature.77+ Omitting the `timeout` parameter will send a request using whatever default78+ timeout has been configured on the client. Including `timeout=None` will79+ ensure no timeout is used.80+81+ Note that user code shouldn't need to use the `USE_CLIENT_DEFAULT` constant,82+ but it is used internally when a parameter is not included.83+ """84+85+ pass # pragma: nocover86+87+88+USE_CLIENT_DEFAULT = UseClientDefault()89+90+91 logger = get_logger(__name__)92 93 USER_AGENT = f"python-httpx/{__version__}"94 ACCEPT_ENCODING = ", ".join(329 unmodified lines424 else:425 raise TypeError(f'Invalid "auth" argument: {auth!r}')426 427 def _build_request_auth(405- self, request: Request, auth: typing.Union[AuthTypes, UnsetType] = UNSET428+ self,429+ request: Request,430+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,431 ) -> Auth:407- auth = self._auth if isinstance(auth, UnsetType) else self._build_auth(auth)432+ auth = (433+ self._auth if isinstance(auth, UseClientDefault) else self._build_auth(auth)434+ )435 436 if auth is not None:437 return auth438 292 unmodified lines731 json: typing.Any = None,732 params: QueryParamTypes = None,733 headers: HeaderTypes = None,734 cookies: CookieTypes = None,708- auth: typing.Union[AuthTypes, UnsetType] = UNSET,735+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,736 allow_redirects: bool = True,710- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,737+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,738 ) -> Response:739 """740 Build and send a request.741 45 unmodified lines787 json: typing.Any = None,788 params: QueryParamTypes = None,789 headers: HeaderTypes = None,790 cookies: CookieTypes = None,764- auth: typing.Union[AuthTypes, UnsetType] = UNSET,791+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,792 allow_redirects: bool = True,766- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,793+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,794 ) -> typing.Iterator[Response]:795 """796 Alternative to `httpx.request()` that streams the response body797 instead of loading it into memory at once.31 unmodified lines829 self,830 request: Request,831 *,832 stream: bool = False,806- auth: typing.Union[AuthTypes, UnsetType] = UNSET,833+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,834 allow_redirects: bool = True,808- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,835+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,836 ) -> Response:837 """838 Send a request.839 10 unmodified lines850 if self._state == ClientState.CLOSED:851 raise RuntimeError("Cannot send a request, as the client has been closed.")852 853 self._state = ClientState.OPENED827- timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout)854+ timeout = (855+ self.timeout if isinstance(timeout, UseClientDefault) else Timeout(timeout)856+ )857 858 auth = self._build_request_auth(request, auth)859 860 response = self._send_handling_auth(130 unmodified lines991 *,992 params: QueryParamTypes = None,993 headers: HeaderTypes = None,994 cookies: CookieTypes = None,966- auth: typing.Union[AuthTypes, UnsetType] = UNSET,995+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,996 allow_redirects: bool = True,968- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,997+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,998 ) -> Response:999 """1000 Send a `GET` request.1001 16 unmodified lines1018 *,1019 params: QueryParamTypes = None,1020 headers: HeaderTypes = None,1021 cookies: CookieTypes = None,993- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1022+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1023 allow_redirects: bool = True,995- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1024+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1025 ) -> Response:1026 """1027 Send an `OPTIONS` request.1028 16 unmodified lines1045 *,1046 params: QueryParamTypes = None,1047 headers: HeaderTypes = None,1048 cookies: CookieTypes = None,1020- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1049+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1050 allow_redirects: bool = True,1022- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1051+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1052 ) -> Response:1053 """1054 Send a `HEAD` request.1055 20 unmodified lines1076 json: typing.Any = None,1077 params: QueryParamTypes = None,1078 headers: HeaderTypes = None,1079 cookies: CookieTypes = None,1051- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1080+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1081 allow_redirects: bool = True,1053- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1082+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1083 ) -> Response:1084 """1085 Send a `POST` request.1086 24 unmodified lines1111 json: typing.Any = None,1112 params: QueryParamTypes = None,1113 headers: HeaderTypes = None,1114 cookies: CookieTypes = None,1086- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1115+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1116 allow_redirects: bool = True,1088- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1117+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1118 ) -> Response:1119 """1120 Send a `PUT` request.1121 24 unmodified lines1146 json: typing.Any = None,1147 params: QueryParamTypes = None,1148 headers: HeaderTypes = None,1149 cookies: CookieTypes = None,1121- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1150+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1151 allow_redirects: bool = True,1123- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1152+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1153 ) -> Response:1154 """1155 Send a `PATCH` request.1156 20 unmodified lines1177 *,1178 params: QueryParamTypes = None,1179 headers: HeaderTypes = None,1180 cookies: CookieTypes = None,1152- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1181+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1182 allow_redirects: bool = True,1154- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1183+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1184 ) -> Response:1185 """1186 Send a `DELETE` request.1187 231 unmodified lines1419 json: typing.Any = None,1420 params: QueryParamTypes = None,1421 headers: HeaderTypes = None,1422 cookies: CookieTypes = None,1394- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1423+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1424 allow_redirects: bool = True,1396- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1425+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1426 ) -> Response:1427 """1428 Build and send a request.1429 38 unmodified lines1468 json: typing.Any = None,1469 params: QueryParamTypes = None,1470 headers: HeaderTypes = None,1471 cookies: CookieTypes = None,1443- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1472+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1473 allow_redirects: bool = True,1445- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1474+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1475 ) -> typing.AsyncIterator[Response]:1476 """1477 Alternative to `httpx.request()` that streams the response body1478 instead of loading it into memory at once.31 unmodified lines1510 self,1511 request: Request,1512 *,1513 stream: bool = False,1485- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1514+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1515 allow_redirects: bool = True,1487- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1516+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1517 ) -> Response:1518 """1519 Send a request.1520 10 unmodified lines1531 if self._state == ClientState.CLOSED:1532 raise RuntimeError("Cannot send a request, as the client has been closed.")1533 1534 self._state = ClientState.OPENED1506- timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout)1535+ timeout = (1536+ self.timeout if isinstance(timeout, UseClientDefault) else Timeout(timeout)1537+ )1538 1539 auth = self._build_request_auth(request, auth)1540 1541 response = await self._send_handling_auth(137 unmodified lines1679 *,1680 params: QueryParamTypes = None,1681 headers: HeaderTypes = None,1682 cookies: CookieTypes = None,1652- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1683+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1684 allow_redirects: bool = True,1654- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1685+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1686 ) -> Response:1687 """1688 Send a `GET` request.1689 16 unmodified lines1706 *,1707 params: QueryParamTypes = None,1708 headers: HeaderTypes = None,1709 cookies: CookieTypes = None,1679- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1710+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1711 allow_redirects: bool = True,1681- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1712+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1713 ) -> Response:1714 """1715 Send an `OPTIONS` request.1716 16 unmodified lines1733 *,1734 params: QueryParamTypes = None,1735 headers: HeaderTypes = None,1736 cookies: CookieTypes = None,1706- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1737+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1738 allow_redirects: bool = True,1708- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1739+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1740 ) -> Response:1741 """1742 Send a `HEAD` request.1743 20 unmodified lines1764 json: typing.Any = None,1765 params: QueryParamTypes = None,1766 headers: HeaderTypes = None,1767 cookies: CookieTypes = None,1737- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1768+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1769 allow_redirects: bool = True,1739- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1770+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1771 ) -> Response:1772 """1773 Send a `POST` request.1774 24 unmodified lines1799 json: typing.Any = None,1800 params: QueryParamTypes = None,1801 headers: HeaderTypes = None,1802 cookies: CookieTypes = None,1772- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1803+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1804 allow_redirects: bool = True,1774- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1805+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1806 ) -> Response:1807 """1808 Send a `PUT` request.1809 24 unmodified lines1834 json: typing.Any = None,1835 params: QueryParamTypes = None,1836 headers: HeaderTypes = None,1837 cookies: CookieTypes = None,1807- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1838+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1839 allow_redirects: bool = True,1809- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1840+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1841 ) -> Response:1842 """1843 Send a `PATCH` request.1844 20 unmodified lines1865 *,1866 params: QueryParamTypes = None,1867 headers: HeaderTypes = None,1868 cookies: CookieTypes = None,1838- auth: typing.Union[AuthTypes, UnsetType] = UNSET,1869+ auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1870 allow_redirects: bool = True,1840- timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1871+ timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT,1872 ) -> Response:1873 """1874 Send a `DELETE` request.1875 74 unmodified lines