Skip to content

Commit 8e24885

Browse files
paramiko: Improve various bytes-related types (#10109)
1 parent 6b5ca0b commit 8e24885

15 files changed

+61
-43
lines changed

stubs/paramiko/paramiko/agent.pyi

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from _typeshed import ReadableBuffer
12
from socket import _RetAddress, socket
23
from threading import Thread
34
from typing import Protocol
45

56
from paramiko.channel import Channel
6-
from paramiko.message import Message
7+
from paramiko.message import Message, _LikeBytes
78
from paramiko.pkey import PKey
89
from paramiko.transport import Transport
910

@@ -61,7 +62,7 @@ class AgentKey(PKey):
6162
blob: bytes
6263
public_blob: None
6364
name: str
64-
def __init__(self, agent: AgentSSH, blob: bytes) -> None: ...
65+
def __init__(self, agent: AgentSSH, blob: ReadableBuffer) -> None: ...
6566
def asbytes(self) -> bytes: ...
6667
def get_name(self) -> str: ...
67-
def sign_ssh_data(self, data: bytes, algorithm: str | None = None) -> Message: ...
68+
def sign_ssh_data(self, data: _LikeBytes, algorithm: str | None = None) -> Message: ...

stubs/paramiko/paramiko/channel.pyi

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
from collections.abc import Callable, Mapping
1+
from _typeshed import SupportsItems
2+
from collections.abc import Callable
23
from logging import Logger
34
from threading import Condition, Event, Lock
45
from typing import Any, TypeVar
56
from typing_extensions import Literal
67

78
from paramiko.buffered_pipe import BufferedPipe
89
from paramiko.file import BufferedFile
10+
from paramiko.message import _LikeBytes
911
from paramiko.transport import Transport
1012
from paramiko.util import ClosingContextManager
1113

@@ -43,22 +45,22 @@ class Channel(ClosingContextManager):
4345
def __init__(self, chanid: int) -> None: ...
4446
def __del__(self) -> None: ...
4547
def get_pty(
46-
self, term: str | bytes = "vt100", width: int = 80, height: int = 24, width_pixels: int = 0, height_pixels: int = 0
48+
self, term: _LikeBytes = "vt100", width: int = 80, height: int = 24, width_pixels: int = 0, height_pixels: int = 0
4749
) -> None: ...
4850
def invoke_shell(self) -> None: ...
49-
def exec_command(self, command: str | bytes) -> None: ...
50-
def invoke_subsystem(self, subsystem: str | bytes) -> None: ...
51+
def exec_command(self, command: _LikeBytes) -> None: ...
52+
def invoke_subsystem(self, subsystem: _LikeBytes) -> None: ...
5153
def resize_pty(self, width: int = 80, height: int = 24, width_pixels: int = 0, height_pixels: int = 0) -> None: ...
52-
def update_environment(self, environment: Mapping[str | bytes, str | bytes]) -> None: ...
53-
def set_environment_variable(self, name: str | bytes, value: str | bytes) -> None: ...
54+
def update_environment(self, environment: SupportsItems[_LikeBytes, _LikeBytes]) -> None: ...
55+
def set_environment_variable(self, name: _LikeBytes, value: _LikeBytes) -> None: ...
5456
def exit_status_ready(self) -> bool: ...
5557
def recv_exit_status(self) -> int: ...
5658
def send_exit_status(self, status: int) -> None: ...
5759
def request_x11(
5860
self,
5961
screen_number: int = 0,
60-
auth_protocol: str | bytes | None = None,
61-
auth_cookie: str | bytes | None = None,
62+
auth_protocol: _LikeBytes | None = None,
63+
auth_cookie: _LikeBytes | None = None,
6264
single_connection: bool = False,
6365
handler: Callable[[Channel, tuple[str, int]], object] | None = None,
6466
) -> bytes: ...
@@ -78,10 +80,10 @@ class Channel(ClosingContextManager):
7880
def recv_stderr_ready(self) -> bool: ...
7981
def recv_stderr(self, nbytes: int) -> bytes: ...
8082
def send_ready(self) -> bool: ...
81-
def send(self, s: bytes) -> int: ...
82-
def send_stderr(self, s: bytes) -> int: ...
83-
def sendall(self, s: bytes) -> None: ...
84-
def sendall_stderr(self, s: bytes) -> None: ...
83+
def send(self, s: bytes | bytearray) -> int: ...
84+
def send_stderr(self, s: bytes | bytearray) -> int: ...
85+
def sendall(self, s: bytes | bytearray) -> None: ...
86+
def sendall_stderr(self, s: bytes | bytearray) -> None: ...
8587
def makefile(self, *params: Any) -> ChannelFile: ...
8688
def makefile_stderr(self, *params: Any) -> ChannelStderrFile: ...
8789
def makefile_stdin(self, *params: Any) -> ChannelStdinFile: ...

stubs/paramiko/paramiko/compress.pyi

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
from _typeshed import ReadableBuffer
12
from zlib import _Compress, _Decompress
23

34
class ZlibCompressor:
45
z: _Compress
56
def __init__(self) -> None: ...
6-
def __call__(self, data: bytes) -> bytes: ...
7+
def __call__(self, data: ReadableBuffer) -> bytes: ...
78

89
class ZlibDecompressor:
910
z: _Decompress
1011
def __init__(self) -> None: ...
11-
def __call__(self, data: bytes) -> bytes: ...
12+
def __call__(self, data: ReadableBuffer) -> bytes: ...

stubs/paramiko/paramiko/dsskey.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import ReadableBuffer
12
from collections.abc import Callable
23
from typing import IO
34

@@ -15,7 +16,7 @@ class DSSKey(PKey):
1516
def __init__(
1617
self,
1718
msg: Message | None = None,
18-
data: bytes | None = None,
19+
data: ReadableBuffer | None = None,
1920
filename: str | None = None,
2021
password: str | None = None,
2122
vals: tuple[int, int, int, int] | None = None,

stubs/paramiko/paramiko/ecdsakey.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import ReadableBuffer
12
from collections.abc import Callable, Sequence
23
from typing import IO, Any
34

@@ -30,7 +31,7 @@ class ECDSAKey(PKey):
3031
def __init__(
3132
self,
3233
msg: Message | None = None,
33-
data: bytes | None = None,
34+
data: ReadableBuffer | None = None,
3435
filename: str | None = None,
3536
password: str | None = None,
3637
vals: tuple[EllipticCurvePrivateKey, EllipticCurvePublicKey] | None = None,

stubs/paramiko/paramiko/ed25519key.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import ReadableBuffer
12
from typing import IO
23

34
from paramiko.message import Message
@@ -8,7 +9,7 @@ class Ed25519Key(PKey):
89
def __init__(
910
self,
1011
msg: Message | None = None,
11-
data: bytes | None = None,
12+
data: ReadableBuffer | None = None,
1213
filename: str | None = None,
1314
password: str | None = None,
1415
file_obj: IO[str] | None = None,

stubs/paramiko/paramiko/message.pyi

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import ReadableBuffer
12
from collections.abc import Iterable
23
from io import BytesIO
34
from typing import Any, Protocol
@@ -6,13 +7,13 @@ from typing_extensions import TypeAlias
67
class _SupportsAsBytes(Protocol):
78
def asbytes(self) -> bytes: ...
89

9-
_LikeBytes: TypeAlias = bytes | str | _SupportsAsBytes
10+
_LikeBytes: TypeAlias = bytes | str | _SupportsAsBytes | ReadableBuffer
1011

1112
class Message:
1213
big_int: int
1314
packet: BytesIO
1415
seqno: int # only when packet.Packetizer.read_message() is used
15-
def __init__(self, content: bytes | None = None) -> None: ...
16+
def __init__(self, content: ReadableBuffer | None = None) -> None: ...
1617
def __bytes__(self) -> bytes: ...
1718
def asbytes(self) -> bytes: ...
1819
def rewind(self) -> None: ...
@@ -29,8 +30,8 @@ class Message:
2930
def get_text(self) -> str: ...
3031
def get_binary(self) -> bytes: ...
3132
def get_list(self) -> list[str]: ...
32-
def add_bytes(self, b: bytes) -> Message: ...
33-
def add_byte(self, b: bytes) -> Message: ...
33+
def add_bytes(self, b: ReadableBuffer) -> Message: ...
34+
def add_byte(self, b: ReadableBuffer) -> Message: ...
3435
def add_boolean(self, b: bool) -> Message: ...
3536
def add_int(self, n: int) -> Message: ...
3637
def add_adaptive_int(self, n: int) -> Message: ...

stubs/paramiko/paramiko/packet.pyi

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from _typeshed import Incomplete
1+
from _typeshed import Incomplete, ReadableBuffer
22
from collections.abc import Callable
33
from hashlib import _Hash
44
from logging import Logger
@@ -9,7 +9,7 @@ from cryptography.hazmat.primitives.ciphers import Cipher
99
from paramiko.compress import ZlibCompressor, ZlibDecompressor
1010
from paramiko.message import Message
1111

12-
def compute_hmac(key: bytes, message: bytes, digest_class: _Hash) -> bytes: ...
12+
def compute_hmac(key: bytes | bytearray, message: ReadableBuffer, digest_class: _Hash) -> bytes: ...
1313

1414
class NeedRekeyException(Exception): ...
1515

@@ -30,7 +30,7 @@ class Packetizer:
3030
block_size: int,
3131
mac_engine: _Hash,
3232
mac_size: int,
33-
mac_key: bytes,
33+
mac_key: bytes | bytearray,
3434
sdctr: bool = False,
3535
etm: bool = False,
3636
) -> None: ...
@@ -40,7 +40,7 @@ class Packetizer:
4040
block_size: int,
4141
mac_engine: _Hash,
4242
mac_size: int,
43-
mac_key: bytes,
43+
mac_key: bytes | bytearray,
4444
etm: bool = False,
4545
) -> None: ...
4646
def set_outbound_compressor(self, compressor: ZlibCompressor) -> None: ...
@@ -57,7 +57,7 @@ class Packetizer:
5757
def handshake_timed_out(self) -> bool: ...
5858
def complete_handshake(self) -> None: ...
5959
def read_all(self, n: int, check_rekey: bool = False) -> bytes: ...
60-
def write_all(self, out: bytes) -> None: ...
60+
def write_all(self, out: ReadableBuffer) -> None: ...
6161
def readline(self, timeout: float) -> str: ...
6262
def send_message(self, data: Message) -> None: ...
6363
def read_message(self) -> tuple[int, Message]: ...

stubs/paramiko/paramiko/pkey.pyi

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from re import Pattern
2-
from typing import IO
2+
from typing import IO, TypeVar
33
from typing_extensions import Self
44

55
from paramiko.message import Message
66

77
OPENSSH_AUTH_MAGIC: bytes
88

9-
def _unpad_openssh(data: bytes) -> bytes: ...
9+
_BytesT = TypeVar("_BytesT", bound=bytes | bytearray)
10+
11+
def _unpad_openssh(data: _BytesT) -> _BytesT: ...
1012

1113
class PKey:
1214
public_blob: PublicBlob | None
@@ -33,7 +35,7 @@ class PKey:
3335

3436
class PublicBlob:
3537
key_type: str
36-
key_blob: str
38+
key_blob: bytes
3739
comment: str
3840
def __init__(self, type_: str, blob: bytes, comment: str | None = None) -> None: ...
3941
@classmethod

stubs/paramiko/paramiko/proxy.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import ReadableBuffer
12
from subprocess import Popen
23
from typing import Any
34

@@ -8,7 +9,7 @@ class ProxyCommand(ClosingContextManager):
89
process: Popen[Any]
910
timeout: float | None
1011
def __init__(self, command_line: str) -> None: ...
11-
def send(self, content: bytes) -> int: ...
12+
def send(self, content: ReadableBuffer) -> int: ...
1213
def recv(self, size: int) -> bytes: ...
1314
def close(self) -> None: ...
1415
@property

stubs/paramiko/paramiko/rsakey.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import ReadableBuffer
12
from collections.abc import Callable
23
from typing import IO
34

@@ -11,7 +12,7 @@ class RSAKey(PKey):
1112
def __init__(
1213
self,
1314
msg: Message | None = None,
14-
data: bytes | None = None,
15+
data: ReadableBuffer | None = None,
1516
filename: str | None = None,
1617
password: str | None = None,
1718
key: None | RSAPublicKey | RSAPrivateKey = None,

stubs/paramiko/paramiko/sftp_client.pyi

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import StrOrBytesPath
12
from collections.abc import Callable, Iterator
23
from logging import Logger
34
from typing import IO
@@ -50,11 +51,11 @@ class SFTPClient(BaseSFTP, ClosingContextManager):
5051
self, fl: IO[bytes], remotepath: bytes | str, file_size: int = 0, callback: _Callback | None = None, confirm: bool = True
5152
) -> SFTPAttributes: ...
5253
def put(
53-
self, localpath: bytes | str, remotepath: bytes | str, callback: _Callback | None = None, confirm: bool = True
54+
self, localpath: StrOrBytesPath, remotepath: bytes | str, callback: _Callback | None = None, confirm: bool = True
5455
) -> SFTPAttributes: ...
5556
def getfo(self, remotepath: bytes | str, fl: IO[bytes], callback: _Callback | None = None, prefetch: bool = True) -> int: ...
5657
def get(
57-
self, remotepath: bytes | str, localpath: bytes | str, callback: _Callback | None = None, prefetch: bool = True
58+
self, remotepath: bytes | str, localpath: StrOrBytesPath, callback: _Callback | None = None, prefetch: bool = True
5859
) -> None: ...
5960

6061
class SFTP(SFTPClient): ...

stubs/paramiko/paramiko/sftp_file.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ from collections.abc import Iterator, Sequence
22
from typing import Any
33

44
from paramiko.file import BufferedFile
5+
from paramiko.message import _LikeBytes
56
from paramiko.sftp_attr import SFTPAttributes
67
from paramiko.sftp_client import SFTPClient
78
from paramiko.sftp_handle import SFTPHandle
@@ -11,7 +12,7 @@ class SFTPFile(BufferedFile[Any]):
1112
sftp: SFTPClient
1213
handle: SFTPHandle
1314
pipelined: bool
14-
def __init__(self, sftp: SFTPClient, handle: bytes, mode: str = "r", bufsize: int = -1) -> None: ...
15+
def __init__(self, sftp: SFTPClient, handle: _LikeBytes, mode: str = "r", bufsize: int = -1) -> None: ...
1516
def __del__(self) -> None: ...
1617
def close(self) -> None: ...
1718
def settimeout(self, timeout: float) -> None: ...
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from _typeshed import ReadableBuffer
2+
13
from paramiko.sftp_attr import SFTPAttributes
24
from paramiko.util import ClosingContextManager
35

46
class SFTPHandle(ClosingContextManager):
57
def __init__(self, flags: int = 0) -> None: ...
68
def close(self) -> None: ...
79
def read(self, offset: int, length: int) -> bytes | int: ...
8-
def write(self, offset: int, data: bytes) -> int: ...
10+
def write(self, offset: int, data: ReadableBuffer) -> int: ...
911
def stat(self) -> int | SFTPAttributes: ...
1012
def chattr(self, attr: SFTPAttributes) -> int: ...

stubs/paramiko/paramiko/util.pyi

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from _typeshed import ReadableBuffer
2+
from collections.abc import Iterable
13
from hashlib import _Hash
24
from logging import Logger, LogRecord
35
from types import TracebackType
@@ -10,14 +12,14 @@ from paramiko.hostkeys import HostKeys
1012
class SupportsClose(Protocol):
1113
def close(self) -> None: ...
1214

13-
def inflate_long(s: bytes, always_positive: bool = False) -> int: ...
15+
def inflate_long(s: bytes | bytearray, always_positive: bool = False) -> int: ...
1416
def deflate_long(n: int, add_sign_padding: bool = True) -> bytes: ...
15-
def format_binary(data: bytes, prefix: str = "") -> list[str]: ...
16-
def format_binary_line(data: bytes) -> str: ...
17-
def safe_string(s: bytes) -> bytes: ...
17+
def format_binary(data: bytes | bytearray, prefix: str = "") -> list[str]: ...
18+
def format_binary_line(data: bytes | bytearray) -> str: ...
19+
def safe_string(s: Iterable[int | str]) -> bytes: ...
1820
def bit_length(n: int) -> int: ...
1921
def tb_strings() -> list[str]: ...
20-
def generate_key_bytes(hash_alg: type[_Hash], salt: bytes, key: bytes | str, nbytes: int) -> bytes: ...
22+
def generate_key_bytes(hash_alg: type[_Hash], salt: ReadableBuffer, key: bytes | str, nbytes: int) -> bytes: ...
2123
def load_host_keys(filename: str) -> HostKeys: ...
2224
def parse_ssh_config(file_obj: IO[str]) -> SSHConfig: ...
2325
def lookup_ssh_host_config(hostname: str, config: SSHConfig) -> SSHConfigDict: ...

0 commit comments

Comments
 (0)