Skip to content

Add type stub for PySocks #14623

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions stubs/PySocks/METADATA.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = "1.7.1"
upstream_repository = "https://github.com/Anorov/PySocks"
124 changes: 124 additions & 0 deletions stubs/PySocks/socks.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import logging
import socket
import types
from _typeshed import ReadableBuffer
from collections.abc import Callable, Iterable, Mapping
from typing import Any, Final, TypeVar
from typing_extensions import ParamSpec, TypeAlias

__version__: Final[str]

log: logging.Logger # undocumented

_ProxyType: TypeAlias = int

PROXY_TYPE_SOCKS4: Final[_ProxyType]
SOCKS4: Final[_ProxyType]
PROXY_TYPE_SOCKS5: Final[_ProxyType]
SOCKS5: Final[_ProxyType]
PROXY_TYPE_HTTP: Final[_ProxyType]
HTTP: Final[_ProxyType]

PROXY_TYPES: Final[dict[str, _ProxyType]]
PRINTABLE_PROXY_TYPES: Final[dict[_ProxyType, str]]

_T = TypeVar("_T")
_P = ParamSpec("_P")

def set_self_blocking(function: Callable[_P, _T]) -> Callable[_P, _T]: ... # undocumented

class ProxyError(IOError):
msg: str
socket_err: socket.error
def __init__(self, msg: str, socket_err: socket.error | None = None) -> None: ...

class GeneralProxyError(ProxyError): ...
class ProxyConnectionError(ProxyError): ...
class SOCKS5AuthError(ProxyError): ...
class SOCKS5Error(ProxyError): ...
class SOCKS4Error(ProxyError): ...
class HTTPError(ProxyError): ...

SOCKS4_ERRORS: Final[Mapping[int, str]]
SOCKS5_ERRORS: Final[Mapping[int, str]]
DEFAULT_PORTS: Final[Mapping[_ProxyType, int]]

_DefaultProxy: TypeAlias = tuple[_ProxyType | None, str | None, int | None, bool, bytes | None, bytes | None]

def set_default_proxy(
proxy_type: _ProxyType | None = None,
addr: str | None = None,
port: int | None = None,
rdns: bool = True,
username: str | None = None,
password: str | None = None,
) -> None: ...
def setdefaultproxy(*args: Any, **kwargs: Any) -> None: ...
def get_default_proxy() -> _DefaultProxy | None: ...

getdefaultproxy = get_default_proxy

def wrap_module(module: types.ModuleType) -> None: ...

wrapmodule = wrap_module

_Endpoint: TypeAlias = tuple[str, int]

def create_connection(
dest_pair: _Endpoint,
timeout: int | None = None,
source_address: _Endpoint | None = None,
proxy_type: _ProxyType | None = None,
proxy_addr: str | None = None,
proxy_port: int | None = None,
proxy_rdns: bool = True,
proxy_username: str | None = None,
proxy_password: str | None = None,
socket_options: (
Iterable[tuple[int, int, int | ReadableBuffer] | tuple[int, int, None, int]] | None
) = None, # values passing to `socket.setsockopt` method
) -> socksocket: ...

class _BaseSocket(socket.socket): # undocumented
def __init__(self, *pos: Any, **kw: Any) -> None: ...

method: object # undocumented
name: object # undocumented

class socksocket(_BaseSocket):
default_proxy: _DefaultProxy | None # undocumented
proxy: _DefaultProxy # undocumented
proxy_sockname: _Endpoint | None # undocumented
proxy_peername: _Endpoint | None # undocumented
def __init__(
self, family: socket.AddressFamily = ..., type: socket.SocketKind = ..., proto: int = 0, *args: Any, **kwargs: Any
) -> None: ...
def settimeout(self, timeout: float | None) -> None: ...
def gettimeout(self) -> float | None: ...
def setblocking(self, v: bool) -> None: ...
def set_proxy(
self,
proxy_type: _ProxyType | None = None,
addr: str | None = None,
port: int | None = None,
rdns: bool = True,
username: str | None = None,
password: str | None = None,
) -> None: ...
def setproxy(self, *args: Any, **kwargs: Any) -> None: ...
def bind(self, *pos: Any, **kw: Any) -> None: ...
def sendto(self, bytes: ReadableBuffer, *args: Any, **kwargs: Any) -> int: ...
def send(self, bytes: ReadableBuffer, flags: int = 0, **kwargs: Any) -> int: ...
def recvfrom(self, bufsize: int, flags: int = 0) -> tuple[bytes, _Endpoint]: ...
def recv(self, *pos: Any, **kw: Any) -> bytes: ...
def close(self) -> None: ...
def get_proxy_sockname(self) -> _Endpoint | None: ...
getproxysockname = get_proxy_sockname
def get_proxy_peername(self) -> _Endpoint | None: ...
getproxypeername = get_proxy_peername
def get_peername(self) -> _Endpoint | None: ...
getpeername = get_peername
@set_self_blocking
def connect(self, dest_pair: _Endpoint, catch_errors: bool | None = None) -> None: ... # type: ignore[override]
@set_self_blocking
def connect_ex(self, dest_pair: _Endpoint) -> int: ... # type: ignore[override]
58 changes: 58 additions & 0 deletions stubs/PySocks/sockshandler.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import http.client
import urllib.request
from _typeshed import Incomplete, SupportsKeysAndGetItem
from typing import Any, TypeVar
from typing_extensions import override

import socks

_K = TypeVar("_K")
_V = TypeVar("_V")

def merge_dict(a: dict[_K, _V], b: SupportsKeysAndGetItem[_K, _V]) -> dict[_K, _V]: ... # undocumented
def is_ip(s: str) -> bool: ... # undocumented

socks4_no_rdns: set[str] # undocumented

class SocksiPyConnection(http.client.HTTPConnection): # undocumented
proxyargs: tuple[int, str, int | None, bool, str | None, str | None]
sock: socks.socksocket
def __init__(
self,
proxytype: int,
proxyaddr: str,
proxyport: int | None = None,
rdns: bool = True,
username: str | None = None,
password: str | None = None,
*args: Any,
**kwargs: Any,
) -> None: ...
@override
def connect(self) -> None: ...

class SocksiPyConnectionS(http.client.HTTPSConnection): # undocumented
proxyargs: tuple[int, str, int | None, bool, str | None, str | None]
sock: socks.socksocket
def __init__(
self,
proxytype: int,
proxyaddr: str,
proxyport: int | None = None,
rdns: bool = True,
username: str | None = None,
password: str | None = None,
*args: Any,
**kwargs: Any,
) -> None: ...
@override
def connect(self) -> None: ...

class SocksiPyHandler(urllib.request.HTTPHandler, urllib.request.HTTPSHandler):
args: tuple[Incomplete, ...] # undocumented
kw: dict[str, Incomplete] # undocumented
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
@override
def http_open(self, req: urllib.request.Request) -> http.client.HTTPResponse: ... # undocumented
@override
def https_open(self, req: urllib.request.Request) -> http.client.HTTPResponse: ... # undocumented
Loading