From 429c139a2c7cc3fb100436a17509a88e19548687 Mon Sep 17 00:00:00 2001 From: Jaakko Lappalainen Date: Mon, 5 Apr 2021 09:56:28 +0000 Subject: [PATCH 1/4] add new variable to default unset params --- httpx/_client.py | 86 +++++++++++++++++++++++++----------------------- httpx/_config.py | 1 + 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/httpx/_client.py b/httpx/_client.py index 7f8ce53101..ed3d336580 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -10,7 +10,7 @@ DEFAULT_LIMITS, DEFAULT_MAX_REDIRECTS, DEFAULT_TIMEOUT_CONFIG, - UNSET, + USE_CLIENT_DEFAULT, Limits, Proxy, Timeout, @@ -251,9 +251,9 @@ def stream( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> "StreamContextManager": """ Alternative to `httpx.request()` that streams the response body @@ -396,7 +396,9 @@ def _build_auth(self, auth: AuthTypes) -> typing.Optional[Auth]: raise TypeError(f'Invalid "auth" argument: {auth!r}') def _build_request_auth( - self, request: Request, auth: typing.Union[AuthTypes, UnsetType] = UNSET + self, + request: Request, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Auth: auth = self._auth if isinstance(auth, UnsetType) else self._build_auth(auth) @@ -707,9 +709,9 @@ def request( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Build and send a request. @@ -747,9 +749,9 @@ def send( request: Request, *, stream: bool = False, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a request. @@ -907,9 +909,9 @@ def get( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `GET` request. @@ -934,9 +936,9 @@ def options( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send an `OPTIONS` request. @@ -961,9 +963,9 @@ def head( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `HEAD` request. @@ -992,9 +994,9 @@ def post( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `POST` request. @@ -1027,9 +1029,9 @@ def put( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `PUT` request. @@ -1062,9 +1064,9 @@ def patch( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `PATCH` request. @@ -1093,9 +1095,9 @@ def delete( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `DELETE` request. @@ -1343,9 +1345,9 @@ async def request( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Build and send a request. @@ -1384,9 +1386,9 @@ async def send( request: Request, *, stream: bool = False, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a request. @@ -1551,9 +1553,9 @@ async def get( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `GET` request. @@ -1578,9 +1580,9 @@ async def options( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send an `OPTIONS` request. @@ -1605,9 +1607,9 @@ async def head( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `HEAD` request. @@ -1636,9 +1638,9 @@ async def post( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `POST` request. @@ -1671,9 +1673,9 @@ async def put( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `PUT` request. @@ -1706,9 +1708,9 @@ async def patch( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `PATCH` request. @@ -1737,9 +1739,9 @@ async def delete( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `DELETE` request. @@ -1826,9 +1828,9 @@ def __init__( client: BaseClient, request: Request, *, - auth: typing.Union[AuthTypes, UnsetType] = UNSET, + auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, close_client: bool = False, ) -> None: self.client = client diff --git a/httpx/_config.py b/httpx/_config.py index 837519afb5..5b79503749 100644 --- a/httpx/_config.py +++ b/httpx/_config.py @@ -38,6 +38,7 @@ class UnsetType: UNSET = UnsetType() +USE_CLIENT_DEFAULT = UnsetType() def create_ssl_context( From 02bc561775aaaf2fb3deaebc5f1b9f6cc0f1f9d2 Mon Sep 17 00:00:00 2001 From: Jaakko Lappalainen Date: Thu, 8 Apr 2021 21:54:06 +0000 Subject: [PATCH 2/4] import variable in __init__ --- httpx/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httpx/__init__.py b/httpx/__init__.py index a441669bf6..5bd3ef528e 100644 --- a/httpx/__init__.py +++ b/httpx/__init__.py @@ -1,7 +1,7 @@ from .__version__ import __description__, __title__, __version__ from ._api import delete, get, head, options, patch, post, put, request, stream from ._auth import Auth, BasicAuth, DigestAuth -from ._client import AsyncClient, Client +from ._client import USE_CLIENT_DEFAULT, AsyncClient, Client from ._config import Limits, Proxy, Timeout, create_ssl_context from ._exceptions import ( CloseError, @@ -102,6 +102,7 @@ "TooManyRedirects", "TransportError", "UnsupportedProtocol", + "USE_CLIENT_DEFAULT", "URL", "WriteError", "WriteTimeout", From 08e0c501f6cbd2c6e8095a874a662b68aea901fd Mon Sep 17 00:00:00 2001 From: Jaakko Lappalainen Date: Thu, 8 Apr 2021 22:00:11 +0000 Subject: [PATCH 3/4] use ClientDefaultType over UnsetType --- httpx/__init__.py | 2 +- httpx/_client.py | 98 +++++++++++++++++++++++++---------------------- httpx/_config.py | 43 +++++++++++---------- 3 files changed, 76 insertions(+), 67 deletions(-) diff --git a/httpx/__init__.py b/httpx/__init__.py index 5bd3ef528e..89c27c1289 100644 --- a/httpx/__init__.py +++ b/httpx/__init__.py @@ -102,8 +102,8 @@ "TooManyRedirects", "TransportError", "UnsupportedProtocol", - "USE_CLIENT_DEFAULT", "URL", + "USE_CLIENT_DEFAULT", "WriteError", "WriteTimeout", "WSGITransport", diff --git a/httpx/_client.py b/httpx/_client.py index ed3d336580..15b7b10e9a 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -11,10 +11,10 @@ DEFAULT_MAX_REDIRECTS, DEFAULT_TIMEOUT_CONFIG, USE_CLIENT_DEFAULT, + ClientDefaultType, Limits, Proxy, Timeout, - UnsetType, ) from ._decoders import SUPPORTED_DECODERS from ._exceptions import ( @@ -251,9 +251,9 @@ def stream( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> "StreamContextManager": """ Alternative to `httpx.request()` that streams the response body @@ -398,9 +398,13 @@ def _build_auth(self, auth: AuthTypes) -> typing.Optional[Auth]: def _build_request_auth( self, request: Request, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Auth: - auth = self._auth if isinstance(auth, UnsetType) else self._build_auth(auth) + auth = ( + self._auth + if isinstance(auth, ClientDefaultType) + else self._build_auth(auth) + ) if auth is not None: return auth @@ -709,9 +713,9 @@ def request( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Build and send a request. @@ -749,9 +753,9 @@ def send( request: Request, *, stream: bool = False, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a request. @@ -770,7 +774,9 @@ def send( raise RuntimeError("Cannot send a request, as the client has been closed.") self._state = ClientState.OPENED - timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout) + timeout = ( + self.timeout if isinstance(timeout, ClientDefaultType) else Timeout(timeout) + ) auth = self._build_request_auth(request, auth) @@ -909,9 +915,9 @@ def get( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `GET` request. @@ -936,9 +942,9 @@ def options( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send an `OPTIONS` request. @@ -963,9 +969,9 @@ def head( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `HEAD` request. @@ -994,9 +1000,9 @@ def post( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `POST` request. @@ -1029,9 +1035,9 @@ def put( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `PUT` request. @@ -1064,9 +1070,9 @@ def patch( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `PATCH` request. @@ -1095,9 +1101,9 @@ def delete( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `DELETE` request. @@ -1345,9 +1351,9 @@ async def request( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Build and send a request. @@ -1386,9 +1392,9 @@ async def send( request: Request, *, stream: bool = False, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a request. @@ -1407,7 +1413,9 @@ async def send( raise RuntimeError("Cannot send a request, as the client has been closed.") self._state = ClientState.OPENED - timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout) + timeout = ( + self.timeout if isinstance(timeout, ClientDefaultType) else Timeout(timeout) + ) auth = self._build_request_auth(request, auth) @@ -1553,9 +1561,9 @@ async def get( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `GET` request. @@ -1580,9 +1588,9 @@ async def options( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send an `OPTIONS` request. @@ -1607,9 +1615,9 @@ async def head( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `HEAD` request. @@ -1638,9 +1646,9 @@ async def post( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `POST` request. @@ -1673,9 +1681,9 @@ async def put( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `PUT` request. @@ -1708,9 +1716,9 @@ async def patch( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `PATCH` request. @@ -1739,9 +1747,9 @@ async def delete( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, ) -> Response: """ Send a `DELETE` request. @@ -1828,9 +1836,9 @@ def __init__( client: BaseClient, request: Request, *, - auth: typing.Union[AuthTypes, UnsetType] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, UnsetType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, close_client: bool = False, ) -> None: self.client = client diff --git a/httpx/_config.py b/httpx/_config.py index 5b79503749..bf4727e296 100644 --- a/httpx/_config.py +++ b/httpx/_config.py @@ -33,12 +33,11 @@ logger = get_logger(__name__) -class UnsetType: +class ClientDefaultType: pass # pragma: nocover -UNSET = UnsetType() -USE_CLIENT_DEFAULT = UnsetType() +USE_CLIENT_DEFAULT = ClientDefaultType() def create_ssl_context( @@ -206,19 +205,19 @@ class Timeout: def __init__( self, - timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, + timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, *, - connect: typing.Union[None, float, UnsetType] = UNSET, - read: typing.Union[None, float, UnsetType] = UNSET, - write: typing.Union[None, float, UnsetType] = UNSET, - pool: typing.Union[None, float, UnsetType] = UNSET, + connect: typing.Union[None, float, ClientDefaultType] = USE_CLIENT_DEFAULT, + read: typing.Union[None, float, ClientDefaultType] = USE_CLIENT_DEFAULT, + write: typing.Union[None, float, ClientDefaultType] = USE_CLIENT_DEFAULT, + pool: typing.Union[None, float, ClientDefaultType] = USE_CLIENT_DEFAULT, ): if isinstance(timeout, Timeout): # Passed as a single explicit Timeout. - assert connect is UNSET - assert read is UNSET - assert write is UNSET - assert pool is UNSET + assert connect is USE_CLIENT_DEFAULT + assert read is USE_CLIENT_DEFAULT + assert write is USE_CLIENT_DEFAULT + assert pool is USE_CLIENT_DEFAULT self.connect = timeout.connect # type: typing.Optional[float] self.read = timeout.read # type: typing.Optional[float] self.write = timeout.write # type: typing.Optional[float] @@ -230,25 +229,27 @@ def __init__( self.write = None if len(timeout) < 3 else timeout[2] self.pool = None if len(timeout) < 4 else timeout[3] elif not ( - isinstance(connect, UnsetType) - or isinstance(read, UnsetType) - or isinstance(write, UnsetType) - or isinstance(pool, UnsetType) + isinstance(connect, ClientDefaultType) + or isinstance(read, ClientDefaultType) + or isinstance(write, ClientDefaultType) + or isinstance(pool, ClientDefaultType) ): self.connect = connect self.read = read self.write = write self.pool = pool else: - if isinstance(timeout, UnsetType): + if isinstance(timeout, ClientDefaultType): raise ValueError( "httpx.Timeout must either include a default, or set all " "four parameters explicitly." ) - self.connect = timeout if isinstance(connect, UnsetType) else connect - self.read = timeout if isinstance(read, UnsetType) else read - self.write = timeout if isinstance(write, UnsetType) else write - self.pool = timeout if isinstance(pool, UnsetType) else pool + self.connect = ( + timeout if isinstance(connect, ClientDefaultType) else connect + ) + self.read = timeout if isinstance(read, ClientDefaultType) else read + self.write = timeout if isinstance(write, ClientDefaultType) else write + self.pool = timeout if isinstance(pool, ClientDefaultType) else pool def as_dict(self) -> typing.Dict[str, typing.Optional[float]]: return { From 5e8bd6aed81950bda436fa512b2315741c0909fc Mon Sep 17 00:00:00 2001 From: Jaakko Lappalainen Date: Fri, 16 Apr 2021 15:38:45 +0000 Subject: [PATCH 4/4] revert typing change for Timeout class --- httpx/_client.py | 50 +++++++++++++++++++++++------------------------- httpx/_config.py | 43 ++++++++++++++++++++++------------------- 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/httpx/_client.py b/httpx/_client.py index bdc2b6cf59..e33597f531 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -10,11 +10,13 @@ DEFAULT_LIMITS, DEFAULT_MAX_REDIRECTS, DEFAULT_TIMEOUT_CONFIG, + UNSET, USE_CLIENT_DEFAULT, ClientDefaultType, Limits, Proxy, Timeout, + UnsetType, ) from ._decoders import SUPPORTED_DECODERS from ._exceptions import ( @@ -303,7 +305,7 @@ def stream( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> "StreamContextManager": """ Alternative to `httpx.request()` that streams the response body @@ -766,7 +768,7 @@ def request( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Build and send a request. @@ -806,7 +808,7 @@ def send( stream: bool = False, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a request. @@ -825,9 +827,7 @@ def send( raise RuntimeError("Cannot send a request, as the client has been closed.") self._state = ClientState.OPENED - timeout = ( - self.timeout if isinstance(timeout, ClientDefaultType) else Timeout(timeout) - ) + timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout) auth = self._build_request_auth(request, auth) @@ -968,7 +968,7 @@ def get( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a `GET` request. @@ -995,7 +995,7 @@ def options( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send an `OPTIONS` request. @@ -1022,7 +1022,7 @@ def head( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a `HEAD` request. @@ -1053,7 +1053,7 @@ def post( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a `POST` request. @@ -1088,7 +1088,7 @@ def put( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a `PUT` request. @@ -1123,7 +1123,7 @@ def patch( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a `PATCH` request. @@ -1154,7 +1154,7 @@ def delete( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a `DELETE` request. @@ -1404,7 +1404,7 @@ async def request( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Build and send a request. @@ -1445,7 +1445,7 @@ async def send( stream: bool = False, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a request. @@ -1464,9 +1464,7 @@ async def send( raise RuntimeError("Cannot send a request, as the client has been closed.") self._state = ClientState.OPENED - timeout = ( - self.timeout if isinstance(timeout, ClientDefaultType) else Timeout(timeout) - ) + timeout = self.timeout if isinstance(timeout, UnsetType) else Timeout(timeout) auth = self._build_request_auth(request, auth) @@ -1614,7 +1612,7 @@ async def get( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a `GET` request. @@ -1641,7 +1639,7 @@ async def options( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send an `OPTIONS` request. @@ -1668,7 +1666,7 @@ async def head( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a `HEAD` request. @@ -1699,7 +1697,7 @@ async def post( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a `POST` request. @@ -1734,7 +1732,7 @@ async def put( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a `PUT` request. @@ -1769,7 +1767,7 @@ async def patch( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a `PATCH` request. @@ -1800,7 +1798,7 @@ async def delete( cookies: CookieTypes = None, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: """ Send a `DELETE` request. @@ -1889,7 +1887,7 @@ def __init__( *, auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, allow_redirects: bool = True, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, close_client: bool = False, ) -> None: self.client = client diff --git a/httpx/_config.py b/httpx/_config.py index bf4727e296..858f4478e4 100644 --- a/httpx/_config.py +++ b/httpx/_config.py @@ -33,10 +33,15 @@ logger = get_logger(__name__) +class UnsetType: + pass #  pragma: nocover + + class ClientDefaultType: pass # pragma: nocover +UNSET = UnsetType() USE_CLIENT_DEFAULT = ClientDefaultType() @@ -205,19 +210,19 @@ class Timeout: def __init__( self, - timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT, + timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, *, - connect: typing.Union[None, float, ClientDefaultType] = USE_CLIENT_DEFAULT, - read: typing.Union[None, float, ClientDefaultType] = USE_CLIENT_DEFAULT, - write: typing.Union[None, float, ClientDefaultType] = USE_CLIENT_DEFAULT, - pool: typing.Union[None, float, ClientDefaultType] = USE_CLIENT_DEFAULT, + connect: typing.Union[None, float, UnsetType] = UNSET, + read: typing.Union[None, float, UnsetType] = UNSET, + write: typing.Union[None, float, UnsetType] = UNSET, + pool: typing.Union[None, float, UnsetType] = UNSET, ): if isinstance(timeout, Timeout): # Passed as a single explicit Timeout. - assert connect is USE_CLIENT_DEFAULT - assert read is USE_CLIENT_DEFAULT - assert write is USE_CLIENT_DEFAULT - assert pool is USE_CLIENT_DEFAULT + assert connect is UNSET + assert read is UNSET + assert write is UNSET + assert pool is UNSET self.connect = timeout.connect # type: typing.Optional[float] self.read = timeout.read # type: typing.Optional[float] self.write = timeout.write # type: typing.Optional[float] @@ -229,27 +234,25 @@ def __init__( self.write = None if len(timeout) < 3 else timeout[2] self.pool = None if len(timeout) < 4 else timeout[3] elif not ( - isinstance(connect, ClientDefaultType) - or isinstance(read, ClientDefaultType) - or isinstance(write, ClientDefaultType) - or isinstance(pool, ClientDefaultType) + isinstance(connect, UnsetType) + or isinstance(read, UnsetType) + or isinstance(write, UnsetType) + or isinstance(pool, UnsetType) ): self.connect = connect self.read = read self.write = write self.pool = pool else: - if isinstance(timeout, ClientDefaultType): + if isinstance(timeout, UnsetType): raise ValueError( "httpx.Timeout must either include a default, or set all " "four parameters explicitly." ) - self.connect = ( - timeout if isinstance(connect, ClientDefaultType) else connect - ) - self.read = timeout if isinstance(read, ClientDefaultType) else read - self.write = timeout if isinstance(write, ClientDefaultType) else write - self.pool = timeout if isinstance(pool, ClientDefaultType) else pool + self.connect = timeout if isinstance(connect, UnsetType) else connect + self.read = timeout if isinstance(read, UnsetType) else read + self.write = timeout if isinstance(write, UnsetType) else write + self.pool = timeout if isinstance(pool, UnsetType) else pool def as_dict(self) -> typing.Dict[str, typing.Optional[float]]: return {