Skip to content

Commit 103c2f3

Browse files
Kevin Kirschepre-commit-ci[bot]AlexWaygood
authored
Add urllib3.contrib.socks; improve urllib3.connectionpool (#8457)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Alex Waygood <[email protected]>
1 parent 799fd2c commit 103c2f3

File tree

5 files changed

+125
-61
lines changed

5 files changed

+125
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PySocks>=1.5.6,<2.0,!=1.5.7

stubs/urllib3/@tests/stubtest_allowlist.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# TODO: remove ResponseCls ignore when https://github.com/python/mypy/issues/13316 is closed
2+
urllib3.HTTPConnectionPool.ResponseCls
13
urllib3.HTTPConnectionPool.__init__
24
urllib3.HTTPConnectionPool.urlopen
35
urllib3.HTTPSConnectionPool.__init__
@@ -17,6 +19,8 @@ urllib3.connection.VerifiedHTTPSConnection.__init__
1719
urllib3.connection.VerifiedHTTPSConnection.set_cert
1820
urllib3.connectionpool.ConnectionError
1921
urllib3.connectionpool.HTTPConnection.request
22+
# TODO: remove ResponseCls ignore when https://github.com/python/mypy/issues/13316 is closed
23+
urllib3.connectionpool.HTTPConnectionPool.ResponseCls
2024
urllib3.connectionpool.HTTPConnectionPool.__init__
2125
urllib3.connectionpool.HTTPConnectionPool.urlopen
2226
urllib3.connectionpool.HTTPSConnection.__init__
+71-61
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
from typing import Any
1+
import queue
2+
from _typeshed import Self
3+
from collections.abc import Mapping
4+
from logging import Logger
5+
from types import TracebackType
6+
from typing import Any, ClassVar
7+
from typing_extensions import Literal, TypeAlias
28

39
from . import connection, exceptions, request, response
410
from .connection import BaseSSLError as BaseSSLError, ConnectionError as ConnectionError, HTTPException as HTTPException
511
from .packages import ssl_match_hostname
6-
from .util import connection as _connection, retry, timeout, url
12+
from .util import Url, connection as _connection, queue as urllib3queue, retry, timeout, url
713

814
ClosedPoolError = exceptions.ClosedPoolError
915
ProtocolError = exceptions.ProtocolError
@@ -29,48 +35,54 @@ Retry = retry.Retry
2935
Timeout = timeout.Timeout
3036
get_host = url.get_host
3137

38+
_Timeout: TypeAlias = Timeout | float
39+
_Retries: TypeAlias = Retry | bool | int
40+
3241
xrange: Any
33-
log: Any
42+
log: Logger
3443

3544
class ConnectionPool:
36-
scheme: Any
37-
QueueCls: Any
38-
host: Any
39-
port: Any
40-
def __init__(self, host, port=...) -> None: ...
41-
def __enter__(self): ...
42-
def __exit__(self, exc_type, exc_val, exc_tb): ...
43-
def close(self): ...
45+
scheme: ClassVar[str | None]
46+
QueueCls: ClassVar[type[queue.Queue[Any]]]
47+
host: str
48+
port: int | None
49+
def __init__(self, host: str, port: int | None = ...) -> None: ...
50+
def __enter__(self: Self) -> Self: ...
51+
def __exit__(
52+
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
53+
) -> Literal[False]: ...
54+
def close(self) -> None: ...
4455

4556
class HTTPConnectionPool(ConnectionPool, RequestMethods):
46-
scheme: Any
47-
ConnectionCls: Any
48-
strict: Any
49-
timeout: Any
50-
retries: Any
51-
pool: Any
52-
block: Any
53-
proxy: Any
54-
proxy_headers: Any
55-
num_connections: Any
56-
num_requests: Any
57+
scheme: ClassVar[str]
58+
ConnectionCls: ClassVar[type[HTTPConnection | HTTPSConnection]]
59+
ResponseCls: ClassVar[type[HTTPResponse]]
60+
strict: bool
61+
timeout: _Timeout
62+
retries: _Retries | None
63+
pool: urllib3queue.LifoQueue | None
64+
block: bool
65+
proxy: Url | None
66+
proxy_headers: Mapping[str, str]
67+
num_connections: int
68+
num_requests: int
5769
conn_kw: Any
5870
def __init__(
5971
self,
60-
host,
61-
port=...,
62-
strict=...,
63-
timeout=...,
64-
maxsize=...,
65-
block=...,
66-
headers=...,
67-
retries=...,
68-
_proxy=...,
69-
_proxy_headers=...,
72+
host: str,
73+
port: int | None = ...,
74+
strict: bool = ...,
75+
timeout: _Timeout = ...,
76+
maxsize: int = ...,
77+
block: bool = ...,
78+
headers: Mapping[str, str] | None = ...,
79+
retries: _Retries | None = ...,
80+
_proxy: Url | None = ...,
81+
_proxy_headers: Mapping[str, str] | None = ...,
7082
**conn_kw,
7183
) -> None: ...
72-
def close(self): ...
73-
def is_same_host(self, url): ...
84+
def close(self) -> None: ...
85+
def is_same_host(self, url: str) -> bool: ...
7486
def urlopen(
7587
self,
7688
method,
@@ -87,35 +99,33 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
8799
): ...
88100

89101
class HTTPSConnectionPool(HTTPConnectionPool):
90-
scheme: Any
91-
ConnectionCls: Any
92-
key_file: Any
93-
cert_file: Any
94-
cert_reqs: Any
95-
ca_certs: Any
96-
ssl_version: Any
97-
assert_hostname: Any
98-
assert_fingerprint: Any
102+
key_file: str | None
103+
cert_file: str | None
104+
cert_reqs: int | str | None
105+
ca_certs: str | None
106+
ssl_version: int | str | None
107+
assert_hostname: str | Literal[False] | None
108+
assert_fingerprint: str | None
99109
def __init__(
100110
self,
101-
host,
102-
port=...,
103-
strict=...,
104-
timeout=...,
105-
maxsize=...,
106-
block=...,
107-
headers=...,
108-
retries=...,
109-
_proxy=...,
110-
_proxy_headers=...,
111-
key_file=...,
112-
cert_file=...,
113-
cert_reqs=...,
114-
ca_certs=...,
115-
ssl_version=...,
116-
assert_hostname=...,
117-
assert_fingerprint=...,
111+
host: str,
112+
port: int | None = ...,
113+
strict: bool = ...,
114+
timeout: _Timeout = ...,
115+
maxsize: int = ...,
116+
block: bool = ...,
117+
headers: Mapping[str, str] | None = ...,
118+
retries: _Retries | None = ...,
119+
_proxy: Url | None = ...,
120+
_proxy_headers: Mapping[str, str] | None = ...,
121+
key_file: str | None = ...,
122+
cert_file: str | None = ...,
123+
cert_reqs: int | str | None = ...,
124+
ca_certs: str | None = ...,
125+
ssl_version: int | str | None = ...,
126+
assert_hostname: str | Literal[False] | None = ...,
127+
assert_fingerprint: str | None = ...,
118128
**conn_kw,
119129
) -> None: ...
120130

121-
def connection_from_url(url, **kw): ...
131+
def connection_from_url(url: str, **kw) -> HTTPConnectionPool: ...
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from collections.abc import Mapping
2+
from typing import ClassVar
3+
from typing_extensions import TypedDict
4+
5+
from ..connection import HTTPConnection, HTTPSConnection
6+
from ..connectionpool import HTTPConnectionPool, HTTPSConnectionPool
7+
from ..poolmanager import PoolManager
8+
9+
class _TYPE_SOCKS_OPTIONS(TypedDict):
10+
socks_version: int
11+
proxy_host: str | None
12+
proxy_port: str | None
13+
username: str | None
14+
password: str | None
15+
rdns: bool
16+
17+
class SOCKSConnection(HTTPConnection):
18+
def __init__(self, _socks_options: _TYPE_SOCKS_OPTIONS, *args, **kwargs) -> None: ...
19+
20+
class SOCKSHTTPSConnection(SOCKSConnection, HTTPSConnection): ...
21+
22+
class SOCKSHTTPConnectionPool(HTTPConnectionPool):
23+
ConnectionCls: ClassVar[type[SOCKSConnection]]
24+
25+
class SOCKSHTTPSConnectionPool(HTTPSConnectionPool):
26+
ConnectionCls: ClassVar[type[SOCKSHTTPSConnection]]
27+
28+
class _ConnectionPoolClasses(TypedDict):
29+
http: type[SOCKSHTTPConnectionPool]
30+
https: type[SOCKSHTTPSConnectionPool]
31+
32+
class SOCKSProxyManager(PoolManager):
33+
# has a class-level default, but is overridden on instances, so not a ClassVar
34+
pool_classes_by_scheme: _ConnectionPoolClasses
35+
proxy_url: str
36+
37+
def __init__(
38+
self,
39+
proxy_url: str,
40+
username: str | None = ...,
41+
password: str | None = ...,
42+
num_pools: int = ...,
43+
headers: Mapping[str, str] | None = ...,
44+
**connection_pool_kw,
45+
) -> None: ...

stubs/urllib3/urllib3/util/queue.pyi

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from queue import Queue
2+
from typing import Any
3+
4+
class LifoQueue(Queue[Any]): ...

0 commit comments

Comments
 (0)