Skip to content
Closed
3 changes: 2 additions & 1 deletion httpx/__init__.py
Original file line number Diff line number Diff line change
@@ -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 ._content import ByteStream
from ._exceptions import (
Expand Down Expand Up @@ -112,6 +112,7 @@
"TransportError",
"UnsupportedProtocol",
"URL",
"USE_CLIENT_DEFAULT",
"WriteError",
"WriteTimeout",
"WSGITransport",
Expand Down
52 changes: 30 additions & 22 deletions httpx/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
DEFAULT_MAX_REDIRECTS,
DEFAULT_TIMEOUT_CONFIG,
UNSET,
USE_CLIENT_DEFAULT,
ClientDefaultType,
Limits,
Proxy,
Timeout,
Expand Down Expand Up @@ -301,7 +303,7 @@ def stream(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> "StreamContextManager":
Expand Down Expand Up @@ -446,9 +448,15 @@ 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, 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
Expand Down Expand Up @@ -758,7 +766,7 @@ def request(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, these cases in _client.py should be:

    timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,

(But the signature of the arguments in the Timeout class should keep using UNSET)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually thinking about this, it's probably also a good idea to move ClientDefaultType and USE_CLIENT_DEFAULT out of _types.py, and into this module, to keep them close to where they're actually used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, these cases in _client.py should be:

    timeout: typing.Union[TimeoutTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,

(But the signature of the arguments in the Timeout class should keep using UNSET)

If I do that I need to also change the the types in the Timeout class, otherwise mypy complains.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll take a look.

) -> Response:
Expand Down Expand Up @@ -798,7 +806,7 @@ def send(
request: Request,
*,
stream: bool = False,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -958,7 +966,7 @@ def get(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand All @@ -985,7 +993,7 @@ def options(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand All @@ -1012,7 +1020,7 @@ def head(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1043,7 +1051,7 @@ def post(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1078,7 +1086,7 @@ def put(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1113,7 +1121,7 @@ def patch(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1144,7 +1152,7 @@ def delete(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1394,7 +1402,7 @@ async def request(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1435,7 +1443,7 @@ async def send(
request: Request,
*,
stream: bool = False,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1602,7 +1610,7 @@ async def get(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand All @@ -1629,7 +1637,7 @@ async def options(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand All @@ -1656,7 +1664,7 @@ async def head(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1687,7 +1695,7 @@ async def post(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1722,7 +1730,7 @@ async def put(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1757,7 +1765,7 @@ async def patch(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1788,7 +1796,7 @@ async def delete(
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
Expand Down Expand Up @@ -1877,7 +1885,7 @@ def __init__(
client: BaseClient,
request: Request,
*,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
auth: typing.Union[AuthTypes, ClientDefaultType] = USE_CLIENT_DEFAULT,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
close_client: bool = False,
Expand Down
5 changes: 5 additions & 0 deletions httpx/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@


class UnsetType:
pass #  pragma: nocover


class ClientDefaultType:
pass # pragma: nocover


UNSET = UnsetType()
USE_CLIENT_DEFAULT = ClientDefaultType()


def create_ssl_context(
Expand Down