Skip to content

Add urllib3.contrib.socks; improve urllib3.connectionpool #8457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1232e08
fix: Add urllib3.contrib.socks types
Aug 1, 2022
56565eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 1, 2022
33e7aa9
fix: optional syntax
Aug 1, 2022
ed27153
fix: use collections.abc instead of typing
Aug 1, 2022
bff6e4f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 1, 2022
5bebd34
fix: add missing dependency and remove `Any`
Aug 1, 2022
78858c8
fix: remove unused import
Aug 1, 2022
aadbe4f
Remove private _new_conn
Aug 2, 2022
2bf6fb2
Fix incorrect use of `HTTPS` when should be `HTTP`
Aug 2, 2022
ef2f122
Fix do not use `ClassVar` as it's overridden on instances
Aug 2, 2022
fc92c1e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 2, 2022
daf1115
fix: various declarations in urllib3.connectionpool
Aug 2, 2022
c283533
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 2, 2022
82952d1
Fix flake8
AlexWaygood Aug 2, 2022
ced3693
fix: correct ConnectionCls on SOCKS connection pools
Aug 2, 2022
00e4f7b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 2, 2022
7faefda
fix flake8 again
AlexWaygood Aug 2, 2022
397d9f2
Merge branch 'master' into refactor/urllib3-socks
AlexWaygood Aug 2, 2022
d8d8d0b
fix: add stubtest allowlist entry with todo comment
Aug 3, 2022
2b94314
fix: add second stubtest ignore with second todo comment for clarity
Aug 3, 2022
5642cab
fix: __exit__ method signature
Aug 3, 2022
4180301
fix: Use LifoQueue from .util.queue
Aug 3, 2022
2c0dc2a
fix: various bugs with specifics in urllib3
Aug 3, 2022
57e975d
Merge branch 'master' into refactor/urllib3-socks
Aug 3, 2022
5a2f486
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 3, 2022
d4ef0ff
fix: use SOCKSHTTPSConnection rather than SOCKSHTTPConnection
Aug 3, 2022
3799ea3
fix: stubtest error
Aug 3, 2022
f951a3e
fix: move typing.Literal to typing_extensions.Literal
Aug 3, 2022
322b30f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 3, 2022
834417b
fix: use type[LifoQueue] instead of ClassVar[LifoQueue]
Aug 3, 2022
3e231f4
refactor: update ConnectionPool.LifoQueue to ClassVar[type[queue.Queu…
Aug 3, 2022
5441d50
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 3, 2022
20ce0e2
fix: rename LifoQueue import
Aug 3, 2022
a17c253
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 3, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stubs/urllib3/@tests/requirements-stubtest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PySocks>=1.5.6,<2.0,!=1.5.7
4 changes: 4 additions & 0 deletions stubs/urllib3/@tests/stubtest_allowlist.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO: remove ResponseCls ignore when https://github.com/python/mypy/issues/13316 is closed
urllib3.HTTPConnectionPool.ResponseCls
urllib3.HTTPConnectionPool.__init__
urllib3.HTTPConnectionPool.urlopen
urllib3.HTTPSConnectionPool.__init__
Expand All @@ -17,6 +19,8 @@ urllib3.connection.VerifiedHTTPSConnection.__init__
urllib3.connection.VerifiedHTTPSConnection.set_cert
urllib3.connectionpool.ConnectionError
urllib3.connectionpool.HTTPConnection.request
# TODO: remove ResponseCls ignore when https://github.com/python/mypy/issues/13316 is closed
urllib3.connectionpool.HTTPConnectionPool.ResponseCls
urllib3.connectionpool.HTTPConnectionPool.__init__
urllib3.connectionpool.HTTPConnectionPool.urlopen
urllib3.connectionpool.HTTPSConnection.__init__
Expand Down
132 changes: 71 additions & 61 deletions stubs/urllib3/urllib3/connectionpool.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from typing import Any
import queue
from _typeshed import Self
from collections.abc import Mapping
from logging import Logger
from types import TracebackType
from typing import Any, ClassVar
from typing_extensions import Literal, TypeAlias

from . import connection, exceptions, request, response
from .connection import BaseSSLError as BaseSSLError, ConnectionError as ConnectionError, HTTPException as HTTPException
from .packages import ssl_match_hostname
from .util import connection as _connection, retry, timeout, url
from .util import Url, connection as _connection, queue as urllib3queue, retry, timeout, url

ClosedPoolError = exceptions.ClosedPoolError
ProtocolError = exceptions.ProtocolError
Expand All @@ -29,48 +35,54 @@ Retry = retry.Retry
Timeout = timeout.Timeout
get_host = url.get_host

_Timeout: TypeAlias = Timeout | float
_Retries: TypeAlias = Retry | bool | int

xrange: Any
log: Any
log: Logger

class ConnectionPool:
scheme: Any
QueueCls: Any
host: Any
port: Any
def __init__(self, host, port=...) -> None: ...
def __enter__(self): ...
def __exit__(self, exc_type, exc_val, exc_tb): ...
def close(self): ...
scheme: ClassVar[str | None]
QueueCls: ClassVar[type[queue.Queue[Any]]]
host: str
port: int | None
def __init__(self, host: str, port: int | None = ...) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
) -> Literal[False]: ...
def close(self) -> None: ...

class HTTPConnectionPool(ConnectionPool, RequestMethods):
scheme: Any
ConnectionCls: Any
strict: Any
timeout: Any
retries: Any
pool: Any
block: Any
proxy: Any
proxy_headers: Any
num_connections: Any
num_requests: Any
scheme: ClassVar[str]
ConnectionCls: ClassVar[type[HTTPConnection | HTTPSConnection]]
ResponseCls: ClassVar[type[HTTPResponse]]
strict: bool
timeout: _Timeout
retries: _Retries | None
pool: urllib3queue.LifoQueue | None
block: bool
proxy: Url | None
proxy_headers: Mapping[str, str]
num_connections: int
num_requests: int
conn_kw: Any
def __init__(
self,
host,
port=...,
strict=...,
timeout=...,
maxsize=...,
block=...,
headers=...,
retries=...,
_proxy=...,
_proxy_headers=...,
host: str,
port: int | None = ...,
strict: bool = ...,
timeout: _Timeout = ...,
maxsize: int = ...,
block: bool = ...,
headers: Mapping[str, str] | None = ...,
retries: _Retries | None = ...,
_proxy: Url | None = ...,
_proxy_headers: Mapping[str, str] | None = ...,
**conn_kw,
) -> None: ...
def close(self): ...
def is_same_host(self, url): ...
def close(self) -> None: ...
def is_same_host(self, url: str) -> bool: ...
def urlopen(
self,
method,
Expand All @@ -87,35 +99,33 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
): ...

class HTTPSConnectionPool(HTTPConnectionPool):
scheme: Any
ConnectionCls: Any
key_file: Any
cert_file: Any
cert_reqs: Any
ca_certs: Any
ssl_version: Any
assert_hostname: Any
assert_fingerprint: Any
key_file: str | None
cert_file: str | None
cert_reqs: int | str | None
ca_certs: str | None
ssl_version: int | str | None
assert_hostname: str | Literal[False] | None
assert_fingerprint: str | None
def __init__(
self,
host,
port=...,
strict=...,
timeout=...,
maxsize=...,
block=...,
headers=...,
retries=...,
_proxy=...,
_proxy_headers=...,
key_file=...,
cert_file=...,
cert_reqs=...,
ca_certs=...,
ssl_version=...,
assert_hostname=...,
assert_fingerprint=...,
host: str,
port: int | None = ...,
strict: bool = ...,
timeout: _Timeout = ...,
maxsize: int = ...,
block: bool = ...,
headers: Mapping[str, str] | None = ...,
retries: _Retries | None = ...,
_proxy: Url | None = ...,
_proxy_headers: Mapping[str, str] | None = ...,
key_file: str | None = ...,
cert_file: str | None = ...,
cert_reqs: int | str | None = ...,
ca_certs: str | None = ...,
ssl_version: int | str | None = ...,
assert_hostname: str | Literal[False] | None = ...,
assert_fingerprint: str | None = ...,
**conn_kw,
) -> None: ...

def connection_from_url(url, **kw): ...
def connection_from_url(url: str, **kw) -> HTTPConnectionPool: ...
45 changes: 45 additions & 0 deletions stubs/urllib3/urllib3/contrib/socks.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from collections.abc import Mapping
from typing import ClassVar
from typing_extensions import TypedDict

from ..connection import HTTPConnection, HTTPSConnection
from ..connectionpool import HTTPConnectionPool, HTTPSConnectionPool
from ..poolmanager import PoolManager

class _TYPE_SOCKS_OPTIONS(TypedDict):
socks_version: int
proxy_host: str | None
proxy_port: str | None
username: str | None
password: str | None
rdns: bool

class SOCKSConnection(HTTPConnection):
def __init__(self, _socks_options: _TYPE_SOCKS_OPTIONS, *args, **kwargs) -> None: ...

class SOCKSHTTPSConnection(SOCKSConnection, HTTPSConnection): ...

class SOCKSHTTPConnectionPool(HTTPConnectionPool):
ConnectionCls: ClassVar[type[SOCKSConnection]]

class SOCKSHTTPSConnectionPool(HTTPSConnectionPool):
ConnectionCls: ClassVar[type[SOCKSHTTPSConnection]]

class _ConnectionPoolClasses(TypedDict):
http: type[SOCKSHTTPConnectionPool]
https: type[SOCKSHTTPSConnectionPool]

class SOCKSProxyManager(PoolManager):
# has a class-level default, but is overridden on instances, so not a ClassVar
pool_classes_by_scheme: _ConnectionPoolClasses
proxy_url: str

def __init__(
self,
proxy_url: str,
username: str | None = ...,
password: str | None = ...,
num_pools: int = ...,
headers: Mapping[str, str] | None = ...,
**connection_pool_kw,
) -> None: ...
4 changes: 4 additions & 0 deletions stubs/urllib3/urllib3/util/queue.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from queue import Queue
from typing import Any

class LifoQueue(Queue[Any]): ...