From a4463d044f370aef186f3c64bd79e236e4df9e2a Mon Sep 17 00:00:00 2001 From: Florimond Manca Date: Tue, 11 Aug 2020 15:18:27 +0200 Subject: [PATCH] Allow disabling auth per-request using `auth=None` (#1115) Co-authored-by: Tom Christie --- httpx/_client.py | 42 ++++++++++++++++++++------------------- httpx/_types.py | 1 + tests/client/test_auth.py | 12 +++++++++++ 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/httpx/_client.py b/httpx/_client.py index 55c544a4..2da95242 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -284,8 +284,10 @@ class BaseClient: return merged_queryparams return params - def _build_auth(self, request: Request, auth: AuthTypes = None) -> Auth: - auth = self.auth if auth is None else auth + def _build_auth( + self, request: Request, auth: typing.Union[AuthTypes, UnsetType] = UNSET + ) -> Auth: + auth = self.auth if isinstance(auth, UnsetType) else auth if auth is not None: if isinstance(auth, tuple): @@ -607,7 +609,7 @@ class Client(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -646,7 +648,7 @@ class Client(BaseClient): request: Request, *, stream: bool = False, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -792,7 +794,7 @@ class Client(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -819,7 +821,7 @@ class Client(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -846,7 +848,7 @@ class Client(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = False, # NOTE: Differs to usual default. timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -876,7 +878,7 @@ class Client(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -909,7 +911,7 @@ class Client(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -942,7 +944,7 @@ class Client(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -972,7 +974,7 @@ class Client(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -1208,7 +1210,7 @@ class AsyncClient(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -1248,7 +1250,7 @@ class AsyncClient(BaseClient): request: Request, *, stream: bool = False, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -1396,7 +1398,7 @@ class AsyncClient(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -1423,7 +1425,7 @@ class AsyncClient(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -1450,7 +1452,7 @@ class AsyncClient(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = False, # NOTE: Differs to usual default. timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -1480,7 +1482,7 @@ class AsyncClient(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -1513,7 +1515,7 @@ class AsyncClient(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -1546,7 +1548,7 @@ class AsyncClient(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: @@ -1576,7 +1578,7 @@ class AsyncClient(BaseClient): params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> Response: diff --git a/httpx/_types.py b/httpx/_types.py index 89a90a2c..437d601c 100644 --- a/httpx/_types.py +++ b/httpx/_types.py @@ -60,6 +60,7 @@ AuthTypes = Union[ Tuple[Union[str, bytes], Union[str, bytes]], Callable[["Request"], "Request"], "Auth", + None, ] RequestData = Union[dict, str, bytes, Iterator[bytes], AsyncIterator[bytes]] diff --git a/tests/client/test_auth.py b/tests/client/test_auth.py index 32b1720f..d4022eb9 100644 --- a/tests/client/test_auth.py +++ b/tests/client/test_auth.py @@ -279,6 +279,18 @@ async def test_trust_env_auth() -> None: } +@pytest.mark.asyncio +async def test_auth_disable_per_request() -> None: + url = "https://example.org/" + auth = ("tomchristie", "password123") + + client = AsyncClient(transport=AsyncMockTransport(), auth=auth) + response = await client.get(url, auth=None) + + assert response.status_code == 200 + assert response.json() == {"auth": None} + + def test_auth_hidden_url() -> None: url = "http://example-username:example-password@example.org/" expected = "URL('http://example-username:[secure]@example.org/')" -- 2.47.3