From 4e11d9cc4f91b821b31d9e1bc0da4b859b10d73f Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 29 Apr 2023 09:22:18 -0700 Subject: [PATCH 1/4] paramiko: Improve various bytes-related types Part of #9006 --- stubs/paramiko/paramiko/agent.pyi | 7 ++++--- stubs/paramiko/paramiko/channel.pyi | 26 +++++++++++++------------ stubs/paramiko/paramiko/compress.pyi | 5 +++-- stubs/paramiko/paramiko/dsskey.pyi | 3 ++- stubs/paramiko/paramiko/ecdsakey.pyi | 3 ++- stubs/paramiko/paramiko/ed25519key.pyi | 3 ++- stubs/paramiko/paramiko/message.pyi | 9 +++++---- stubs/paramiko/paramiko/packet.pyi | 10 +++++----- stubs/paramiko/paramiko/pkey.pyi | 6 ++++-- stubs/paramiko/paramiko/proxy.pyi | 3 ++- stubs/paramiko/paramiko/rsakey.pyi | 3 ++- stubs/paramiko/paramiko/sftp_client.pyi | 5 +++-- stubs/paramiko/paramiko/sftp_file.pyi | 3 ++- stubs/paramiko/paramiko/sftp_handle.pyi | 3 ++- stubs/paramiko/paramiko/util.pyi | 13 +++++++------ 15 files changed, 59 insertions(+), 43 deletions(-) diff --git a/stubs/paramiko/paramiko/agent.pyi b/stubs/paramiko/paramiko/agent.pyi index 464b0c612c2d..e7cbf16e0422 100644 --- a/stubs/paramiko/paramiko/agent.pyi +++ b/stubs/paramiko/paramiko/agent.pyi @@ -1,9 +1,10 @@ +from _typeshed import ReadableBuffer from socket import _RetAddress, socket from threading import Thread from typing import Protocol from paramiko.channel import Channel -from paramiko.message import Message +from paramiko.message import Message, _LikeBytes from paramiko.pkey import PKey from paramiko.transport import Transport @@ -61,7 +62,7 @@ class AgentKey(PKey): blob: bytes public_blob: None name: str - def __init__(self, agent: AgentSSH, blob: bytes) -> None: ... + def __init__(self, agent: AgentSSH, blob: ReadableBuffer) -> None: ... def asbytes(self) -> bytes: ... def get_name(self) -> str: ... - def sign_ssh_data(self, data: bytes, algorithm: str | None = None) -> Message: ... + def sign_ssh_data(self, data: _LikeBytes, algorithm: str | None = None) -> Message: ... diff --git a/stubs/paramiko/paramiko/channel.pyi b/stubs/paramiko/paramiko/channel.pyi index 536d3bfdc4dc..6b5a3664f7fc 100644 --- a/stubs/paramiko/paramiko/channel.pyi +++ b/stubs/paramiko/paramiko/channel.pyi @@ -1,4 +1,5 @@ -from collections.abc import Callable, Mapping +from _typeshed import SupportsItems +from collections.abc import Callable from logging import Logger from threading import Condition, Event, Lock from typing import Any, TypeVar @@ -6,6 +7,7 @@ from typing_extensions import Literal from paramiko.buffered_pipe import BufferedPipe from paramiko.file import BufferedFile +from paramiko.message import _LikeBytes from paramiko.transport import Transport from paramiko.util import ClosingContextManager @@ -43,22 +45,22 @@ class Channel(ClosingContextManager): def __init__(self, chanid: int) -> None: ... def __del__(self) -> None: ... def get_pty( - self, term: str | bytes = "vt100", width: int = 80, height: int = 24, width_pixels: int = 0, height_pixels: int = 0 + self, term: _LikeBytes = "vt100", width: int = 80, height: int = 24, width_pixels: int = 0, height_pixels: int = 0 ) -> None: ... def invoke_shell(self) -> None: ... - def exec_command(self, command: str | bytes) -> None: ... - def invoke_subsystem(self, subsystem: str | bytes) -> None: ... + def exec_command(self, command: _LikeBytes) -> None: ... + def invoke_subsystem(self, subsystem: _LikeBytes) -> None: ... def resize_pty(self, width: int = 80, height: int = 24, width_pixels: int = 0, height_pixels: int = 0) -> None: ... - def update_environment(self, environment: Mapping[str | bytes, str | bytes]) -> None: ... - def set_environment_variable(self, name: str | bytes, value: str | bytes) -> None: ... + def update_environment(self, environment: SupportsItems[_LikeBytes, _LikeBytes]) -> None: ... + def set_environment_variable(self, name: _LikeBytes, value: _LikeBytes) -> None: ... def exit_status_ready(self) -> bool: ... def recv_exit_status(self) -> int: ... def send_exit_status(self, status: int) -> None: ... def request_x11( self, screen_number: int = 0, - auth_protocol: str | bytes | None = None, - auth_cookie: str | bytes | None = None, + auth_protocol: _LikeBytes | None = None, + auth_cookie: _LikeBytes | None = None, single_connection: bool = False, handler: Callable[[Channel, tuple[str, int]], object] | None = None, ) -> bytes: ... @@ -78,10 +80,10 @@ class Channel(ClosingContextManager): def recv_stderr_ready(self) -> bool: ... def recv_stderr(self, nbytes: int) -> bytes: ... def send_ready(self) -> bool: ... - def send(self, s: bytes) -> int: ... - def send_stderr(self, s: bytes) -> int: ... - def sendall(self, s: bytes) -> None: ... - def sendall_stderr(self, s: bytes) -> None: ... + def send(self, s: bytes | bytearray) -> int: ... + def send_stderr(self, s: bytes | bytearray) -> int: ... + def sendall(self, s: bytes | bytearray) -> None: ... + def sendall_stderr(self, s: bytes | bytearray) -> None: ... def makefile(self, *params: Any) -> ChannelFile: ... def makefile_stderr(self, *params: Any) -> ChannelStderrFile: ... def makefile_stdin(self, *params: Any) -> ChannelStdinFile: ... diff --git a/stubs/paramiko/paramiko/compress.pyi b/stubs/paramiko/paramiko/compress.pyi index da039479745c..9dd81d17f4f5 100644 --- a/stubs/paramiko/paramiko/compress.pyi +++ b/stubs/paramiko/paramiko/compress.pyi @@ -1,11 +1,12 @@ +from _typeshed import ReadableBuffer from zlib import _Compress, _Decompress class ZlibCompressor: z: _Compress def __init__(self) -> None: ... - def __call__(self, data: bytes) -> bytes: ... + def __call__(self, data: ReadableBuffer) -> bytes: ... class ZlibDecompressor: z: _Decompress def __init__(self) -> None: ... - def __call__(self, data: bytes) -> bytes: ... + def __call__(self, data: ReadableBuffer) -> bytes: ... diff --git a/stubs/paramiko/paramiko/dsskey.pyi b/stubs/paramiko/paramiko/dsskey.pyi index a63e8f31a593..979812821299 100644 --- a/stubs/paramiko/paramiko/dsskey.pyi +++ b/stubs/paramiko/paramiko/dsskey.pyi @@ -1,3 +1,4 @@ +from _typeshed import ReadableBuffer from collections.abc import Callable from typing import IO @@ -15,7 +16,7 @@ class DSSKey(PKey): def __init__( self, msg: Message | None = None, - data: bytes | None = None, + data: ReadableBuffer | None = None, filename: str | None = None, password: str | None = None, vals: tuple[int, int, int, int] | None = None, diff --git a/stubs/paramiko/paramiko/ecdsakey.pyi b/stubs/paramiko/paramiko/ecdsakey.pyi index 661b27de69ac..515e7e85ed2b 100644 --- a/stubs/paramiko/paramiko/ecdsakey.pyi +++ b/stubs/paramiko/paramiko/ecdsakey.pyi @@ -1,3 +1,4 @@ +from _typeshed import ReadableBuffer from collections.abc import Callable, Sequence from typing import IO, Any @@ -30,7 +31,7 @@ class ECDSAKey(PKey): def __init__( self, msg: Message | None = None, - data: bytes | None = None, + data: ReadableBuffer | None = None, filename: str | None = None, password: str | None = None, vals: tuple[EllipticCurvePrivateKey, EllipticCurvePublicKey] | None = None, diff --git a/stubs/paramiko/paramiko/ed25519key.pyi b/stubs/paramiko/paramiko/ed25519key.pyi index 8c0361d4ea4a..d5d269a1251b 100644 --- a/stubs/paramiko/paramiko/ed25519key.pyi +++ b/stubs/paramiko/paramiko/ed25519key.pyi @@ -1,3 +1,4 @@ +from _typeshed import ReadableBuffer from typing import IO from paramiko.message import Message @@ -8,7 +9,7 @@ class Ed25519Key(PKey): def __init__( self, msg: Message | None = None, - data: bytes | None = None, + data: ReadableBuffer | None = None, filename: str | None = None, password: str | None = None, file_obj: IO[str] | None = None, diff --git a/stubs/paramiko/paramiko/message.pyi b/stubs/paramiko/paramiko/message.pyi index 0f1261c85808..ac6a9decd1f4 100644 --- a/stubs/paramiko/paramiko/message.pyi +++ b/stubs/paramiko/paramiko/message.pyi @@ -1,3 +1,4 @@ +from _typeshed import ReadableBuffer from collections.abc import Iterable from io import BytesIO from typing import Any, Protocol @@ -6,13 +7,13 @@ from typing_extensions import TypeAlias class _SupportsAsBytes(Protocol): def asbytes(self) -> bytes: ... -_LikeBytes: TypeAlias = bytes | str | _SupportsAsBytes +_LikeBytes: TypeAlias = bytes | str | _SupportsAsBytes | ReadableBuffer class Message: big_int: int packet: BytesIO seqno: int # only when packet.Packetizer.read_message() is used - def __init__(self, content: bytes | None = None) -> None: ... + def __init__(self, content: ReadableBuffer | None = None) -> None: ... def __bytes__(self) -> bytes: ... def asbytes(self) -> bytes: ... def rewind(self) -> None: ... @@ -29,8 +30,8 @@ class Message: def get_text(self) -> str: ... def get_binary(self) -> bytes: ... def get_list(self) -> list[str]: ... - def add_bytes(self, b: bytes) -> Message: ... - def add_byte(self, b: bytes) -> Message: ... + def add_bytes(self, b: ReadableBuffer) -> Message: ... + def add_byte(self, b: ReadableBuffer) -> Message: ... def add_boolean(self, b: bool) -> Message: ... def add_int(self, n: int) -> Message: ... def add_adaptive_int(self, n: int) -> Message: ... diff --git a/stubs/paramiko/paramiko/packet.pyi b/stubs/paramiko/paramiko/packet.pyi index da6a0c9efbab..820d4e241da0 100644 --- a/stubs/paramiko/paramiko/packet.pyi +++ b/stubs/paramiko/paramiko/packet.pyi @@ -1,4 +1,4 @@ -from _typeshed import Incomplete +from _typeshed import Incomplete, ReadableBuffer from collections.abc import Callable from hashlib import _Hash from logging import Logger @@ -9,7 +9,7 @@ from cryptography.hazmat.primitives.ciphers import Cipher from paramiko.compress import ZlibCompressor, ZlibDecompressor from paramiko.message import Message -def compute_hmac(key: bytes, message: bytes, digest_class: _Hash) -> bytes: ... +def compute_hmac(key: bytes | bytearray, message: ReadableBuffer, digest_class: _Hash) -> bytes: ... class NeedRekeyException(Exception): ... @@ -30,7 +30,7 @@ class Packetizer: block_size: int, mac_engine: _Hash, mac_size: int, - mac_key: bytes, + mac_key: bytes | bytearray, sdctr: bool = False, etm: bool = False, ) -> None: ... @@ -40,7 +40,7 @@ class Packetizer: block_size: int, mac_engine: _Hash, mac_size: int, - mac_key: bytes, + mac_key: bytes | bytearray, etm: bool = False, ) -> None: ... def set_outbound_compressor(self, compressor: ZlibCompressor) -> None: ... @@ -57,7 +57,7 @@ class Packetizer: def handshake_timed_out(self) -> bool: ... def complete_handshake(self) -> None: ... def read_all(self, n: int, check_rekey: bool = False) -> bytes: ... - def write_all(self, out: bytes) -> None: ... + def write_all(self, out: ReadableBuffer) -> None: ... def readline(self, timeout: float) -> str: ... def send_message(self, data: Message) -> None: ... def read_message(self) -> tuple[int, Message]: ... diff --git a/stubs/paramiko/paramiko/pkey.pyi b/stubs/paramiko/paramiko/pkey.pyi index a71727411199..5f278386151b 100644 --- a/stubs/paramiko/paramiko/pkey.pyi +++ b/stubs/paramiko/paramiko/pkey.pyi @@ -1,12 +1,14 @@ from re import Pattern -from typing import IO +from typing import IO, TypeVar from typing_extensions import Self from paramiko.message import Message OPENSSH_AUTH_MAGIC: bytes -def _unpad_openssh(data: bytes) -> bytes: ... +_BytesT = TypeVar("_BytesT", bound=bytes | bytearray) + +def _unpad_openssh(data: _BytesT) -> _BytesT: ... class PKey: public_blob: PublicBlob | None diff --git a/stubs/paramiko/paramiko/proxy.pyi b/stubs/paramiko/paramiko/proxy.pyi index 111e056b0c14..ad7971947f37 100644 --- a/stubs/paramiko/paramiko/proxy.pyi +++ b/stubs/paramiko/paramiko/proxy.pyi @@ -1,3 +1,4 @@ +from _typeshed import ReadableBuffer from subprocess import Popen from typing import Any @@ -8,7 +9,7 @@ class ProxyCommand(ClosingContextManager): process: Popen[Any] timeout: float | None def __init__(self, command_line: str) -> None: ... - def send(self, content: bytes) -> int: ... + def send(self, content: ReadableBuffer) -> int: ... def recv(self, size: int) -> bytes: ... def close(self) -> None: ... @property diff --git a/stubs/paramiko/paramiko/rsakey.pyi b/stubs/paramiko/paramiko/rsakey.pyi index b9ececeebcb6..3ad9e14be20f 100644 --- a/stubs/paramiko/paramiko/rsakey.pyi +++ b/stubs/paramiko/paramiko/rsakey.pyi @@ -1,3 +1,4 @@ +from _typeshed import ReadableBuffer from collections.abc import Callable from typing import IO @@ -11,7 +12,7 @@ class RSAKey(PKey): def __init__( self, msg: Message | None = None, - data: bytes | None = None, + data: ReadableBuffer | None = None, filename: str | None = None, password: str | None = None, key: None | RSAPublicKey | RSAPrivateKey = None, diff --git a/stubs/paramiko/paramiko/sftp_client.pyi b/stubs/paramiko/paramiko/sftp_client.pyi index 5bad906be3ac..460c37c05f67 100644 --- a/stubs/paramiko/paramiko/sftp_client.pyi +++ b/stubs/paramiko/paramiko/sftp_client.pyi @@ -1,3 +1,4 @@ +from _typeshed import StrOrBytesPath from collections.abc import Callable, Iterator from logging import Logger from typing import IO @@ -50,11 +51,11 @@ class SFTPClient(BaseSFTP, ClosingContextManager): self, fl: IO[bytes], remotepath: bytes | str, file_size: int = 0, callback: _Callback | None = None, confirm: bool = True ) -> SFTPAttributes: ... def put( - self, localpath: bytes | str, remotepath: bytes | str, callback: _Callback | None = None, confirm: bool = True + self, localpath: StrOrBytesPath, remotepath: bytes | str, callback: _Callback | None = None, confirm: bool = True ) -> SFTPAttributes: ... def getfo(self, remotepath: bytes | str, fl: IO[bytes], callback: _Callback | None = None, prefetch: bool = True) -> int: ... def get( - self, remotepath: bytes | str, localpath: bytes | str, callback: _Callback | None = None, prefetch: bool = True + self, remotepath: bytes | str, localpath: StrOrBytesPath, callback: _Callback | None = None, prefetch: bool = True ) -> None: ... class SFTP(SFTPClient): ... diff --git a/stubs/paramiko/paramiko/sftp_file.pyi b/stubs/paramiko/paramiko/sftp_file.pyi index cbb20b8009b1..afe4bfd47510 100644 --- a/stubs/paramiko/paramiko/sftp_file.pyi +++ b/stubs/paramiko/paramiko/sftp_file.pyi @@ -2,6 +2,7 @@ from collections.abc import Iterator, Sequence from typing import Any from paramiko.file import BufferedFile +from paramiko.message import _LikeBytes from paramiko.sftp_attr import SFTPAttributes from paramiko.sftp_client import SFTPClient from paramiko.sftp_handle import SFTPHandle @@ -11,7 +12,7 @@ class SFTPFile(BufferedFile[Any]): sftp: SFTPClient handle: SFTPHandle pipelined: bool - def __init__(self, sftp: SFTPClient, handle: bytes, mode: str = "r", bufsize: int = -1) -> None: ... + def __init__(self, sftp: SFTPClient, handle: _LikeBytes, mode: str = "r", bufsize: int = -1) -> None: ... def __del__(self) -> None: ... def close(self) -> None: ... def settimeout(self, timeout: float) -> None: ... diff --git a/stubs/paramiko/paramiko/sftp_handle.pyi b/stubs/paramiko/paramiko/sftp_handle.pyi index 08c199d3c107..3a1028e39afb 100644 --- a/stubs/paramiko/paramiko/sftp_handle.pyi +++ b/stubs/paramiko/paramiko/sftp_handle.pyi @@ -1,3 +1,4 @@ +from _typeshed import ReadableBuffer from paramiko.sftp_attr import SFTPAttributes from paramiko.util import ClosingContextManager @@ -5,6 +6,6 @@ class SFTPHandle(ClosingContextManager): def __init__(self, flags: int = 0) -> None: ... def close(self) -> None: ... def read(self, offset: int, length: int) -> bytes | int: ... - def write(self, offset: int, data: bytes) -> int: ... + def write(self, offset: int, data: ReadableBuffer) -> int: ... def stat(self) -> int | SFTPAttributes: ... def chattr(self, attr: SFTPAttributes) -> int: ... diff --git a/stubs/paramiko/paramiko/util.pyi b/stubs/paramiko/paramiko/util.pyi index f8cfecb210ca..d581fc1c74ce 100644 --- a/stubs/paramiko/paramiko/util.pyi +++ b/stubs/paramiko/paramiko/util.pyi @@ -1,7 +1,8 @@ +from _typeshed import ReadableBuffer from hashlib import _Hash from logging import Logger, LogRecord from types import TracebackType -from typing import IO, AnyStr, Protocol +from typing import IO, AnyStr, Iterable, Protocol from typing_extensions import Self from paramiko.config import SSHConfig, SSHConfigDict @@ -10,14 +11,14 @@ from paramiko.hostkeys import HostKeys class SupportsClose(Protocol): def close(self) -> None: ... -def inflate_long(s: bytes, always_positive: bool = False) -> int: ... +def inflate_long(s: bytes | bytearray, always_positive: bool = False) -> int: ... def deflate_long(n: int, add_sign_padding: bool = True) -> bytes: ... -def format_binary(data: bytes, prefix: str = "") -> list[str]: ... -def format_binary_line(data: bytes) -> str: ... -def safe_string(s: bytes) -> bytes: ... +def format_binary(data: bytes | bytearray, prefix: str = "") -> list[str]: ... +def format_binary_line(data: bytes | bytearray) -> str: ... +def safe_string(s: Iterable[int | str]) -> bytes: ... def bit_length(n: int) -> int: ... def tb_strings() -> list[str]: ... -def generate_key_bytes(hash_alg: type[_Hash], salt: bytes, key: bytes | str, nbytes: int) -> bytes: ... +def generate_key_bytes(hash_alg: type[_Hash], salt: ReadableBuffer, key: bytes | str, nbytes: int) -> bytes: ... def load_host_keys(filename: str) -> HostKeys: ... def parse_ssh_config(file_obj: IO[str]) -> SSHConfig: ... def lookup_ssh_host_config(hostname: str, config: SSHConfig) -> SSHConfigDict: ... From 9775ae7387c2d5983aacd61bc5934e4d9bee17f1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 29 Apr 2023 16:23:53 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stubs/paramiko/paramiko/sftp_handle.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stubs/paramiko/paramiko/sftp_handle.pyi b/stubs/paramiko/paramiko/sftp_handle.pyi index 3a1028e39afb..d730c69c4cdb 100644 --- a/stubs/paramiko/paramiko/sftp_handle.pyi +++ b/stubs/paramiko/paramiko/sftp_handle.pyi @@ -1,4 +1,5 @@ from _typeshed import ReadableBuffer + from paramiko.sftp_attr import SFTPAttributes from paramiko.util import ClosingContextManager From 72ce687e845c38a7e53983cf84aece1a26c41378 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 29 Apr 2023 09:25:16 -0700 Subject: [PATCH 3/4] One more fix --- stubs/paramiko/paramiko/pkey.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/paramiko/paramiko/pkey.pyi b/stubs/paramiko/paramiko/pkey.pyi index 5f278386151b..caba24194232 100644 --- a/stubs/paramiko/paramiko/pkey.pyi +++ b/stubs/paramiko/paramiko/pkey.pyi @@ -35,7 +35,7 @@ class PKey: class PublicBlob: key_type: str - key_blob: str + key_blob: bytes comment: str def __init__(self, type_: str, blob: bytes, comment: str | None = None) -> None: ... @classmethod From 0a2d3b97d798862180c1a7ded804a99f4cd8b578 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 29 Apr 2023 09:34:31 -0700 Subject: [PATCH 4/4] PEP 585 --- stubs/paramiko/paramiko/util.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stubs/paramiko/paramiko/util.pyi b/stubs/paramiko/paramiko/util.pyi index d581fc1c74ce..f7388e665337 100644 --- a/stubs/paramiko/paramiko/util.pyi +++ b/stubs/paramiko/paramiko/util.pyi @@ -1,8 +1,9 @@ from _typeshed import ReadableBuffer +from collections.abc import Iterable from hashlib import _Hash from logging import Logger, LogRecord from types import TracebackType -from typing import IO, AnyStr, Iterable, Protocol +from typing import IO, AnyStr, Protocol from typing_extensions import Self from paramiko.config import SSHConfig, SSHConfigDict