Allow disabling auth per-request using `auth=None` (#1115)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Florimond Manca · committed by GitHub · parent db4e417 · Browse files · 3 files·+35 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Tom Christie <tom@tomchristie.com>
Diff stats
3 files+35 added−20 removed
- tests:+12 added−0 removed
- other:+23 added−20 removed
Provenance
- Commit
a4463d0- Tree
eb1e091- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
db4e417
3 files changed+35 added−20 removed
modified:httpx/_client.py+22 added-20 removed
282 unmodified lines283 merged_queryparams.update(params)284 return merged_queryparams285 return params286 287- def _build_auth(self, request: Request, auth: AuthTypes = None) -> Auth:288- auth = self.auth if auth is None else auth287+ def _build_auth(288+ self, request: Request, auth: typing.Union[AuthTypes, UnsetType] = UNSET289+ ) -> Auth:290+ auth = self.auth if isinstance(auth, UnsetType) else auth291 292 if auth is not None:293 if isinstance(auth, tuple):294 return BasicAuth(username=auth[0], password=auth[1])313 unmodified lines608 json: typing.Any = None,609 params: QueryParamTypes = None,610 headers: HeaderTypes = None,611 cookies: CookieTypes = None,610- auth: AuthTypes = None,612+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,613 allow_redirects: bool = True,614 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,615 ) -> Response:616 """30 unmodified lines647 self,648 request: Request,649 *,650 stream: bool = False,649- auth: AuthTypes = None,651+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,652 allow_redirects: bool = True,653 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,654 ) -> Response:655 """137 unmodified lines793 *,794 params: QueryParamTypes = None,795 headers: HeaderTypes = None,796 cookies: CookieTypes = None,795- auth: AuthTypes = None,797+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,798 allow_redirects: bool = True,799 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,800 ) -> Response:801 """18 unmodified lines820 *,821 params: QueryParamTypes = None,822 headers: HeaderTypes = None,823 cookies: CookieTypes = None,822- auth: AuthTypes = None,824+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,825 allow_redirects: bool = True,826 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,827 ) -> Response:828 """18 unmodified lines847 *,848 params: QueryParamTypes = None,849 headers: HeaderTypes = None,850 cookies: CookieTypes = None,849- auth: AuthTypes = None,851+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,852 allow_redirects: bool = False, # NOTE: Differs to usual default.853 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,854 ) -> Response:855 """21 unmodified lines877 json: typing.Any = None,878 params: QueryParamTypes = None,879 headers: HeaderTypes = None,880 cookies: CookieTypes = None,879- auth: AuthTypes = None,881+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,882 allow_redirects: bool = True,883 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,884 ) -> Response:885 """24 unmodified lines910 json: typing.Any = None,911 params: QueryParamTypes = None,912 headers: HeaderTypes = None,913 cookies: CookieTypes = None,912- auth: AuthTypes = None,914+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,915 allow_redirects: bool = True,916 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,917 ) -> Response:918 """24 unmodified lines943 json: typing.Any = None,944 params: QueryParamTypes = None,945 headers: HeaderTypes = None,946 cookies: CookieTypes = None,945- auth: AuthTypes = None,947+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,948 allow_redirects: bool = True,949 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,950 ) -> Response:951 """21 unmodified lines973 *,974 params: QueryParamTypes = None,975 headers: HeaderTypes = None,976 cookies: CookieTypes = None,975- auth: AuthTypes = None,977+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,978 allow_redirects: bool = True,979 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,980 ) -> Response:981 """227 unmodified lines1209 json: typing.Any = None,1210 params: QueryParamTypes = None,1211 headers: HeaderTypes = None,1212 cookies: CookieTypes = None,1211- auth: AuthTypes = None,1213+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,1214 allow_redirects: bool = True,1215 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1216 ) -> Response:1217 """31 unmodified lines1249 self,1250 request: Request,1251 *,1252 stream: bool = False,1251- auth: AuthTypes = None,1253+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,1254 allow_redirects: bool = True,1255 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1256 ) -> Response:1257 """139 unmodified lines1397 *,1398 params: QueryParamTypes = None,1399 headers: HeaderTypes = None,1400 cookies: CookieTypes = None,1399- auth: AuthTypes = None,1401+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,1402 allow_redirects: bool = True,1403 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1404 ) -> Response:1405 """18 unmodified lines1424 *,1425 params: QueryParamTypes = None,1426 headers: HeaderTypes = None,1427 cookies: CookieTypes = None,1426- auth: AuthTypes = None,1428+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,1429 allow_redirects: bool = True,1430 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1431 ) -> Response:1432 """18 unmodified lines1451 *,1452 params: QueryParamTypes = None,1453 headers: HeaderTypes = None,1454 cookies: CookieTypes = None,1453- auth: AuthTypes = None,1455+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,1456 allow_redirects: bool = False, # NOTE: Differs to usual default.1457 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1458 ) -> Response:1459 """21 unmodified lines1481 json: typing.Any = None,1482 params: QueryParamTypes = None,1483 headers: HeaderTypes = None,1484 cookies: CookieTypes = None,1483- auth: AuthTypes = None,1485+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,1486 allow_redirects: bool = True,1487 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1488 ) -> Response:1489 """24 unmodified lines1514 json: typing.Any = None,1515 params: QueryParamTypes = None,1516 headers: HeaderTypes = None,1517 cookies: CookieTypes = None,1516- auth: AuthTypes = None,1518+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,1519 allow_redirects: bool = True,1520 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1521 ) -> Response:1522 """24 unmodified lines1547 json: typing.Any = None,1548 params: QueryParamTypes = None,1549 headers: HeaderTypes = None,1550 cookies: CookieTypes = None,1549- auth: AuthTypes = None,1551+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,1552 allow_redirects: bool = True,1553 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1554 ) -> Response:1555 """21 unmodified lines1577 *,1578 params: QueryParamTypes = None,1579 headers: HeaderTypes = None,1580 cookies: CookieTypes = None,1579- auth: AuthTypes = None,1581+ auth: typing.Union[AuthTypes, UnsetType] = UNSET,1582 allow_redirects: bool = True,1583 timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,1584 ) -> Response:1585 """95 unmodified linesmodified:httpx/_types.py+1 added
58 unmodified lines59 AuthTypes = Union[60 Tuple[Union[str, bytes], Union[str, bytes]],61 Callable[["Request"], "Request"],62 "Auth",63+ None,64 ]65 66 RequestData = Union[dict, str, bytes, Iterator[bytes], AsyncIterator[bytes]]67 10 unmodified linesmodified:tests/client/test_auth.py+12 added
277 unmodified lines278 "auth": "Basic ZXhhbXBsZS11c2VybmFtZTpleGFtcGxlLXBhc3N3b3Jk"279 }280 281 282+@pytest.mark.asyncio283+async def test_auth_disable_per_request() -> None:284+ url = "https://example.org/"285+ auth = ("tomchristie", "password123")286+287+ client = AsyncClient(transport=AsyncMockTransport(), auth=auth)288+ response = await client.get(url, auth=None)289+290+ assert response.status_code == 200291+ assert response.json() == {"auth": None}292+293+294 def test_auth_hidden_url() -> None:295 url = "http://example-username:example-password@example.org/"296 expected = "URL('http://example-username:[secure]@example.org/')"297 assert url == URL(url)325 unmodified lines