Skip to content

Commit 61107f2

Browse files
authored
Add type stub for PySocks (#14623)
1 parent dd31a8a commit 61107f2

4 files changed

Lines changed: 250 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Internal variables that were improperly leaked to the outside
2+
socks.method
3+
socks.name

stubs/PySocks/METADATA.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version = "1.7.1"
2+
upstream_repository = "https://github.com/Anorov/PySocks"

stubs/PySocks/socks.pyi

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import logging
2+
import socket
3+
import types
4+
from _typeshed import ReadableBuffer
5+
from collections.abc import Callable, Iterable, Mapping
6+
from typing import Final, TypeVar, overload
7+
from typing_extensions import ParamSpec, TypeAlias
8+
9+
__version__: Final[str]
10+
11+
log: logging.Logger # undocumented
12+
13+
_ProxyType: TypeAlias = int
14+
15+
PROXY_TYPE_SOCKS4: Final[_ProxyType]
16+
SOCKS4: Final[_ProxyType]
17+
PROXY_TYPE_SOCKS5: Final[_ProxyType]
18+
SOCKS5: Final[_ProxyType]
19+
PROXY_TYPE_HTTP: Final[_ProxyType]
20+
HTTP: Final[_ProxyType]
21+
22+
PROXY_TYPES: Final[dict[str, _ProxyType]]
23+
PRINTABLE_PROXY_TYPES: Final[dict[_ProxyType, str]]
24+
25+
_T = TypeVar("_T")
26+
_P = ParamSpec("_P")
27+
28+
def set_self_blocking(function: Callable[_P, _T]) -> Callable[_P, _T]: ... # undocumented
29+
30+
class ProxyError(IOError):
31+
msg: str
32+
socket_err: socket.error
33+
def __init__(self, msg: str, socket_err: socket.error | None = None) -> None: ...
34+
35+
class GeneralProxyError(ProxyError): ...
36+
class ProxyConnectionError(ProxyError): ...
37+
class SOCKS5AuthError(ProxyError): ...
38+
class SOCKS5Error(ProxyError): ...
39+
class SOCKS4Error(ProxyError): ...
40+
class HTTPError(ProxyError): ...
41+
42+
SOCKS4_ERRORS: Final[Mapping[int, str]]
43+
SOCKS5_ERRORS: Final[Mapping[int, str]]
44+
DEFAULT_PORTS: Final[Mapping[_ProxyType, int]]
45+
46+
_DefaultProxy: TypeAlias = tuple[_ProxyType | None, str | None, int | None, bool, bytes | None, bytes | None]
47+
48+
def set_default_proxy(
49+
proxy_type: _ProxyType | None = None,
50+
addr: str | None = None,
51+
port: int | None = None,
52+
rdns: bool = True,
53+
username: str | None = None,
54+
password: str | None = None,
55+
) -> None: ...
56+
def setdefaultproxy(
57+
proxy_type: _ProxyType | None = None,
58+
addr: str | None = None,
59+
port: int | None = None,
60+
rdns: bool = True,
61+
username: str | None = None,
62+
password: str | None = None,
63+
*,
64+
proxytype: _ProxyType = ...,
65+
) -> None: ...
66+
def get_default_proxy() -> _DefaultProxy | None: ...
67+
68+
getdefaultproxy = get_default_proxy
69+
70+
def wrap_module(module: types.ModuleType) -> None: ...
71+
72+
wrapmodule = wrap_module
73+
74+
_Endpoint: TypeAlias = tuple[str, int]
75+
76+
def create_connection(
77+
dest_pair: _Endpoint,
78+
timeout: int | None = None,
79+
source_address: _Endpoint | None = None,
80+
proxy_type: _ProxyType | None = None,
81+
proxy_addr: str | None = None,
82+
proxy_port: int | None = None,
83+
proxy_rdns: bool = True,
84+
proxy_username: str | None = None,
85+
proxy_password: str | None = None,
86+
socket_options: (
87+
Iterable[tuple[int, int, int | ReadableBuffer] | tuple[int, int, None, int]] | None
88+
) = None, # values passing to `socket.setsockopt` method
89+
) -> socksocket: ...
90+
91+
class _BaseSocket(socket.socket): # undocumented
92+
...
93+
94+
class socksocket(_BaseSocket):
95+
default_proxy: _DefaultProxy | None # undocumented
96+
proxy: _DefaultProxy # undocumented
97+
proxy_sockname: _Endpoint | None # undocumented
98+
proxy_peername: _Endpoint | None # undocumented
99+
def __init__(
100+
self, family: socket.AddressFamily = ..., type: socket.SocketKind = ..., proto: int = 0, fileno: int | None = None
101+
) -> None: ...
102+
def settimeout(self, timeout: float | None) -> None: ...
103+
def gettimeout(self) -> float | None: ...
104+
def setblocking(self, v: bool) -> None: ...
105+
def set_proxy(
106+
self,
107+
proxy_type: _ProxyType | None = None,
108+
addr: str | None = None,
109+
port: int | None = None,
110+
rdns: bool = True,
111+
username: str | None = None,
112+
password: str | None = None,
113+
) -> None: ...
114+
def setproxy(
115+
self,
116+
proxy_type: _ProxyType | None = None,
117+
addr: str | None = None,
118+
port: int | None = None,
119+
rdns: bool = True,
120+
username: str | None = None,
121+
password: str | None = None,
122+
*,
123+
proxytype: _ProxyType = ...,
124+
) -> None: ...
125+
def bind(self, address: socket._Address, /) -> None: ...
126+
@overload
127+
def sendto(self, bytes: ReadableBuffer, address: socket._Address) -> int: ...
128+
@overload
129+
def sendto(self, bytes: ReadableBuffer, flags: int, address: socket._Address) -> int: ...
130+
def send(self, bytes: ReadableBuffer, flags: int = 0) -> int: ...
131+
def recvfrom(self, bufsize: int, flags: int = 0) -> tuple[bytes, _Endpoint]: ...
132+
def recv(self, bufsize: int, flags: int = 0) -> bytes: ...
133+
def close(self) -> None: ...
134+
def get_proxy_sockname(self) -> _Endpoint | None: ...
135+
getproxysockname = get_proxy_sockname
136+
def get_proxy_peername(self) -> _Endpoint | None: ...
137+
getproxypeername = get_proxy_peername
138+
def get_peername(self) -> _Endpoint | None: ...
139+
getpeername = get_peername
140+
@set_self_blocking
141+
def connect(self, dest_pair: _Endpoint, catch_errors: bool | None = None) -> None: ... # type: ignore[override]
142+
@set_self_blocking
143+
def connect_ex(self, dest_pair: _Endpoint) -> int: ... # type: ignore[override]

stubs/PySocks/sockshandler.pyi

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import http.client
2+
import ssl
3+
import sys
4+
import urllib.request
5+
from _typeshed import Incomplete, SupportsKeysAndGetItem
6+
from typing import Any, TypeVar
7+
from typing_extensions import override
8+
9+
import socks
10+
11+
_K = TypeVar("_K")
12+
_V = TypeVar("_V")
13+
14+
def merge_dict(a: dict[_K, _V], b: SupportsKeysAndGetItem[_K, _V]) -> dict[_K, _V]: ... # undocumented
15+
def is_ip(s: str) -> bool: ... # undocumented
16+
17+
socks4_no_rdns: set[str] # undocumented
18+
19+
class SocksiPyConnection(http.client.HTTPConnection): # undocumented
20+
proxyargs: tuple[int, str, int | None, bool, str | None, str | None]
21+
sock: socks.socksocket
22+
def __init__(
23+
self,
24+
proxytype: int,
25+
proxyaddr: str,
26+
proxyport: int | None = None,
27+
rdns: bool = True,
28+
username: str | None = None,
29+
password: str | None = None,
30+
host: str | None = None,
31+
port: int | None = None,
32+
timeout: float | None = ...,
33+
source_address: tuple[str, int] | None = None,
34+
blocksize: int = 8192,
35+
) -> None: ...
36+
@override
37+
def connect(self) -> None: ...
38+
39+
class SocksiPyConnectionS(http.client.HTTPSConnection): # undocumented
40+
proxyargs: tuple[int, str, int | None, bool, str | None, str | None]
41+
sock: socks.socksocket
42+
if sys.version_info >= (3, 12):
43+
def __init__(
44+
self,
45+
proxytype: int,
46+
proxyaddr: str,
47+
proxyport: int | None = None,
48+
rdns: bool = True,
49+
username: str | None = None,
50+
password: str | None = None,
51+
host: str | None = None,
52+
port: int | None = None,
53+
*,
54+
timeout: float | None = ...,
55+
source_address: tuple[str, int] | None = None,
56+
context: ssl.SSLContext | None = None,
57+
blocksize: int = 8192,
58+
) -> None: ...
59+
else:
60+
def __init__(
61+
self,
62+
proxytype: int,
63+
proxyaddr: str,
64+
proxyport: int | None = None,
65+
rdns: bool = True,
66+
username: str | None = None,
67+
password: str | None = None,
68+
host: str | None = None,
69+
port: int | None = None,
70+
key_file: str | None = None,
71+
cert_file: str | None = None,
72+
timeout: float | None = ...,
73+
source_address: tuple[str, int] | None = None,
74+
*,
75+
context: ssl.SSLContext | None = None,
76+
check_hostname: bool | None = None,
77+
blocksize: int = 8192,
78+
) -> None: ...
79+
80+
@override
81+
def connect(self) -> None: ...
82+
83+
class SocksiPyHandler(urllib.request.HTTPHandler, urllib.request.HTTPSHandler):
84+
args: tuple[Incomplete, ...] # undocumented
85+
kw: dict[str, Incomplete] # undocumented
86+
def __init__(
87+
self,
88+
proxytype: int,
89+
proxyaddr: str,
90+
proxyport: int | None = None,
91+
rdns: bool = True,
92+
username: str | None = None,
93+
password: str | None = None,
94+
*,
95+
source_address: tuple[str, int] | None = None,
96+
blocksize: int = 8192,
97+
**kwargs: Any, # any additional arguments to `SocksiPyConnection` or `SocksiPyConnectionS`
98+
) -> None: ...
99+
@override
100+
def http_open(self, req: urllib.request.Request) -> http.client.HTTPResponse: ... # undocumented
101+
@override
102+
def https_open(self, req: urllib.request.Request) -> http.client.HTTPResponse: ... # undocumented

0 commit comments

Comments
 (0)