From 215eff44d9a9843f72a3a651b15cd675cf84b51d Mon Sep 17 00:00:00 2001 From: keicodex Date: Fri, 4 Dec 2026 12:14:04 +0700 Subject: [PATCH] docs: add docstrings to transports, streams, Proxy, DigestAuth and create_ssl_context Refs #3355. Covers the public API classes that had no docstrings, complementary to the module/Request/Response docstrings in #3864. Adds test_public_api_docstrings to tests/test_exported_members.py. --- httpx/_auth.py | 5 +++ httpx/_config.py | 30 +++++++++++++ httpx/_content.py | 5 +++ httpx/_transports/base.py | 14 ++++++ httpx/_transports/default.py | 78 ++++++++++++++++++++++++++++++++++ httpx/_transports/mock.py | 17 ++++++++ httpx/_types.py | 16 +++++++ tests/test_exported_members.py | 21 +++++++++ 8 files changed, 186 insertions(+) diff --git a/httpx/_auth.py b/httpx/_auth.py index 9d24faed99..bf9b7f2cb2 100644 --- a/httpx/_auth.py +++ b/httpx/_auth.py @@ -173,6 +173,11 @@ def _build_auth_header(self, username: str | bytes, password: str | bytes) -> st class DigestAuth(Auth): + """ + Allows the 'auth' argument to be passed as a (username, password) pair, + and uses HTTP Digest authentication. + """ + _ALGORITHM_TO_HASH_FUNCTION: dict[str, typing.Callable[[bytes], _Hash]] = { "MD5": hashlib.md5, "MD5-SESS": hashlib.md5, diff --git a/httpx/_config.py b/httpx/_config.py index 467a6c90ae..e9556717f6 100644 --- a/httpx/_config.py +++ b/httpx/_config.py @@ -25,6 +25,20 @@ def create_ssl_context( cert: CertTypes | None = None, trust_env: bool = True, ) -> ssl.SSLContext: + """ + Create an SSL context, as used by clients and transports to verify + server certificates. + + **Parameters:** + + * **verify** - Either `True` to use an SSL context with the default + CA bundle, `False` to disable verification, or an instance of + `ssl.SSLContext` to use a custom context. + * **cert** - *(optional)* An SSL certificate used by the client when + connecting to the server, to provide mutual TLS authentication. + * **trust_env** - *(optional)* Enables or disables usage of environment + variables for configuration. + """ import ssl import warnings @@ -199,6 +213,22 @@ def __repr__(self) -> str: class Proxy: + """ + Configuration for a proxy to route requests through. + + **Parameters:** + + * **url** - The proxy URL, which must use one of the `"http"`, `"https"`, + `"socks5"`, or `"socks5h"` schemes. Any authentication credentials + included in the URL are treated as the proxy authentication. + * **ssl_context** - *(optional)* An SSL context to use when connecting + to the proxy. + * **auth** - *(optional)* A (username, password) pair to use for + proxy authentication. + * **headers** - *(optional)* Headers to include when connecting + to the proxy. + """ + def __init__( self, url: URL | str, diff --git a/httpx/_content.py b/httpx/_content.py index 6f479a0885..fa201ce81c 100644 --- a/httpx/_content.py +++ b/httpx/_content.py @@ -29,6 +29,11 @@ class ByteStream(AsyncByteStream, SyncByteStream): + """ + A byte stream wrapping an in-memory `bytes` value, that can be used + as either a synchronous or an asynchronous stream. + """ + def __init__(self, stream: bytes) -> None: self._stream = stream diff --git a/httpx/_transports/base.py b/httpx/_transports/base.py index 66fd99d702..67fd593b0b 100644 --- a/httpx/_transports/base.py +++ b/httpx/_transports/base.py @@ -12,6 +12,13 @@ class BaseTransport: + """ + Base class for all synchronous transports. + + To implement a custom transport, subclass `BaseTransport` and override + the `.handle_request()` method. + """ + def __enter__(self: T) -> T: return self @@ -63,6 +70,13 @@ def close(self) -> None: class AsyncBaseTransport: + """ + Base class for all asynchronous transports. + + To implement a custom transport, subclass `AsyncBaseTransport` and + override the `.handle_async_request()` method. + """ + async def __aenter__(self: A) -> A: return self diff --git a/httpx/_transports/default.py b/httpx/_transports/default.py index fc8c70970a..1841d26847 100644 --- a/httpx/_transports/default.py +++ b/httpx/_transports/default.py @@ -133,6 +133,45 @@ def close(self) -> None: class HTTPTransport(BaseTransport): + """ + A synchronous transport that handles sending requests over the network, + using a connection pool. + + This is the default transport used by `Client`. It may also be + instantiated directly, in order to use any additional configuration + that is specific to this transport. + + ```python + transport = httpx.HTTPTransport(retries=1) + client = httpx.Client(transport=transport) + ``` + + **Parameters:** + + * **verify** - *(optional)* Either `True` to use an SSL context with the + default CA bundle, `False` to disable verification, or an instance of + `ssl.SSLContext` to use a custom context. + * **cert** - *(optional)* An SSL certificate used by the client when + connecting to the server, to provide mutual TLS authentication. + * **trust_env** - *(optional)* Enables or disables usage of environment + variables for configuration. + * **http1** - *(optional)* A boolean indicating if HTTP/1.1 support + should be enabled. Defaults to `True`. + * **http2** - *(optional)* A boolean indicating if HTTP/2 support + should be enabled. Defaults to `False`. + * **limits** - *(optional)* The limits configuration to use. + * **proxy** - *(optional)* A proxy URL where all the traffic should + be routed. + * **uds** - *(optional)* A path to a Unix domain socket to use + instead of a TCP connection. + * **local_address** - *(optional)* A local IP address to use for + outgoing connections. + * **retries** - *(optional)* The maximum number of times to retry + a failed connection attempt. + * **socket_options** - *(optional)* Additional socket options + to apply to outgoing connections. + """ + def __init__( self, verify: ssl.SSLContext | str | bool = True, @@ -277,6 +316,45 @@ async def aclose(self) -> None: class AsyncHTTPTransport(AsyncBaseTransport): + """ + An asynchronous transport that handles sending requests over the network, + using a connection pool. + + This is the default transport used by `AsyncClient`. It may also be + instantiated directly, in order to use any additional configuration + that is specific to this transport. + + ```python + transport = httpx.AsyncHTTPTransport(retries=1) + client = httpx.AsyncClient(transport=transport) + ``` + + **Parameters:** + + * **verify** - *(optional)* Either `True` to use an SSL context with the + default CA bundle, `False` to disable verification, or an instance of + `ssl.SSLContext` to use a custom context. + * **cert** - *(optional)* An SSL certificate used by the client when + connecting to the server, to provide mutual TLS authentication. + * **trust_env** - *(optional)* Enables or disables usage of environment + variables for configuration. + * **http1** - *(optional)* A boolean indicating if HTTP/1.1 support + should be enabled. Defaults to `True`. + * **http2** - *(optional)* A boolean indicating if HTTP/2 support + should be enabled. Defaults to `False`. + * **limits** - *(optional)* The limits configuration to use. + * **proxy** - *(optional)* A proxy URL where all the traffic should + be routed. + * **uds** - *(optional)* A path to a Unix domain socket to use + instead of a TCP connection. + * **local_address** - *(optional)* A local IP address to use for + outgoing connections. + * **retries** - *(optional)* The maximum number of times to retry + a failed connection attempt. + * **socket_options** - *(optional)* Additional socket options + to apply to outgoing connections. + """ + def __init__( self, verify: ssl.SSLContext | str | bool = True, diff --git a/httpx/_transports/mock.py b/httpx/_transports/mock.py index 8c418f59e0..023a06cf5f 100644 --- a/httpx/_transports/mock.py +++ b/httpx/_transports/mock.py @@ -13,6 +13,23 @@ class MockTransport(AsyncBaseTransport, BaseTransport): + """ + A transport that maps requests to responses, using a custom handler, + without making any actual network requests. + + Useful for testing, stubbing, or otherwise mocking out network + behaviour. The handler may be either a function or an async function, + for use with `Client` or `AsyncClient` respectively. + + ```python + def handler(request): + return httpx.Response(200, json={"text": "Hello, world!"}) + + transport = httpx.MockTransport(handler) + client = httpx.Client(transport=transport) + ``` + """ + def __init__(self, handler: SyncHandler | AsyncHandler) -> None: self.handler = handler diff --git a/httpx/_types.py b/httpx/_types.py index 704dfdffc8..c9e25d505d 100644 --- a/httpx/_types.py +++ b/httpx/_types.py @@ -90,6 +90,14 @@ class SyncByteStream: + """ + A synchronous byte stream, such as the stream used for the content + of a request or a response. + + To implement a custom stream, subclass `SyncByteStream` and override + the `.__iter__()` method. + """ + def __iter__(self) -> Iterator[bytes]: raise NotImplementedError( "The '__iter__' method must be implemented." @@ -104,6 +112,14 @@ def close(self) -> None: class AsyncByteStream: + """ + An asynchronous byte stream, such as the stream used for the content + of a request or a response. + + To implement a custom stream, subclass `AsyncByteStream` and override + the `.__aiter__()` method. + """ + async def __aiter__(self) -> AsyncIterator[bytes]: raise NotImplementedError( "The '__aiter__' method must be implemented." diff --git a/tests/test_exported_members.py b/tests/test_exported_members.py index 8d9c8a74ca..63dd7daf94 100644 --- a/tests/test_exported_members.py +++ b/tests/test_exported_members.py @@ -11,3 +11,24 @@ def test_all_imports_are_exported() -> None: ), key=str.casefold, ) + + +def test_public_api_docstrings() -> None: + # The transports, streams, and configuration classes that make up the + # public API should all include docstrings, so that they are properly + # documented in IDE autocompletion and hover help. See issue #3355. + members = [ + httpx.AsyncBaseTransport, + httpx.AsyncByteStream, + httpx.AsyncHTTPTransport, + httpx.BaseTransport, + httpx.ByteStream, + httpx.DigestAuth, + httpx.HTTPTransport, + httpx.MockTransport, + httpx.Proxy, + httpx.SyncByteStream, + httpx.create_ssl_context, + ] + for member in members: + assert member.__doc__ and member.__doc__.strip(), member.__name__