Skip to content

Commit 0c702b5

Browse files
authored
[requests] Improve type annotations for several HTTPAdapter methods (#15778)
1 parent 45ecbe3 commit 0c702b5

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

stubs/requests/requests/adapters.pyi

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from _typeshed import Incomplete
22
from collections.abc import Mapping
33
from ssl import SSLContext
4-
from typing import Literal, TypedDict, type_check_only
4+
from typing import Any, Literal, TypedDict, type_check_only
55
from typing_extensions import NotRequired, deprecated
66

77
import urllib3
@@ -80,8 +80,27 @@ class HTTPAdapter(BaseAdapter):
8080
self, pool_connections: int = 10, pool_maxsize: int = 10, max_retries: Retry | int | None = 0, pool_block: bool = False
8181
) -> None: ...
8282
poolmanager: Incomplete
83-
def init_poolmanager(self, connections, maxsize, block=False, **pool_kwargs): ...
84-
def proxy_manager_for(self, proxy, **proxy_kwargs): ...
83+
def init_poolmanager(
84+
self,
85+
connections: int,
86+
maxsize: int,
87+
block: bool = False,
88+
**pool_kwargs: Any, # Any: Arbitrary keyword arguments passed directly to urllib3's PoolManager constructor.
89+
# Allowed types depend on urllib3 version, but typically include:
90+
# ssl_version (int), cert_reqs (str), ca_certs (str), ca_cert_dir (str),
91+
# ssl_context (ssl.SSLContext), socket_options (list), etc.
92+
# We use Any because the exact set is dynamic and not fully specified in stubs.
93+
) -> None: ...
94+
def proxy_manager_for(
95+
self,
96+
proxy: str,
97+
**proxy_kwargs: Any, # Any: Same as pool_kwargs above, passed to ProxyManager or SOCKSProxyManager.
98+
# May include: ssl_context, cert_reqs, ca_certs, ca_cert_dir, etc.
99+
) -> Any: # Any: Returns either urllib3.ProxyManager (for HTTP/HTTPS proxies) or SOCKSProxyManager (for SOCKS).
100+
# The exact return type depends on the proxy scheme and is not needed by callers; using Any avoids
101+
# circular imports or complex union types. In practice, the object adheres to a common interface.
102+
...
103+
85104
def cert_verify(self, conn, url, verify, cert): ...
86105
def build_response(self, req: PreparedRequest, resp: urllib3.BaseHTTPResponse) -> Response: ...
87106
def build_connection_pool_key_attributes(
@@ -97,9 +116,16 @@ class HTTPAdapter(BaseAdapter):
97116
@deprecated("Use get_connection_with_tls_context() instead.")
98117
def get_connection(self, url: _Uri, proxies: Mapping[str, str] | None = None) -> ConnectionPool: ...
99118
def close(self) -> None: ...
100-
def request_url(self, request, proxies): ...
101-
def add_headers(self, request, **kwargs): ...
102-
def proxy_headers(self, proxy): ...
119+
def request_url(self, request: PreparedRequest, proxies: Mapping[str, str] | None) -> str: ...
120+
def add_headers(
121+
self,
122+
request: PreparedRequest,
123+
**kwargs: Any, # Any: Hook method for subclasses to add custom headers.
124+
# The kwargs mirror the send() parameters: stream (bool), timeout (float|tuple),
125+
# verify (bool|str), cert (str|tuple), proxies (dict). Base implementation ignores them.
126+
# Using Any allows subclasses to access these arguments without repeating the full signature.
127+
) -> None: ...
128+
def proxy_headers(self, proxy: str) -> dict[str, str]: ...
103129
def send(
104130
self,
105131
request: PreparedRequest,

stubs/requests/requests/utils.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ def dotted_netmask(mask: int) -> str: ...
4747
def is_ipv4_address(string_ip: str) -> bool: ...
4848
def is_valid_cidr(string_network: str) -> bool: ...
4949
def set_environ(env_name: str, value: None) -> _GeneratorContextManager[None]: ...
50-
def should_bypass_proxies(url: _Uri, no_proxy: Iterable[str] | None) -> bool: ...
50+
def should_bypass_proxies(url: _Uri, no_proxy: str | None) -> bool: ...
5151
def get_environ_proxies(url: _Uri, no_proxy: Iterable[str] | None = None) -> dict[Incomplete, Incomplete]: ...
52-
def select_proxy(url: _Uri, proxies: Mapping[str, str] | None) -> str: ...
52+
def select_proxy(url: _Uri, proxies: Mapping[str, str] | None) -> str | None: ...
5353
def resolve_proxies(
5454
request: Request | PreparedRequest, proxies: dict[str, str] | None, trust_env: bool = True
5555
) -> dict[str, str]: ...

0 commit comments

Comments
 (0)