Skip to content

Commit 4e11d9c

Browse files
committed
paramiko: Improve various bytes-related types
Part of python#9006
1 parent f42f665 commit 4e11d9c

15 files changed

Lines changed: 59 additions & 43 deletions

‎stubs/paramiko/paramiko/agent.pyi‎

Lines changed: 4 additions & 3 deletions
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‎

Lines changed: 14 additions & 12 deletions
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: ...
Lines changed: 3 additions & 2 deletions
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‎

Lines changed: 2 additions & 1 deletion
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‎

Lines changed: 2 additions & 1 deletion
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‎

Lines changed: 2 additions & 1 deletion
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‎

Lines changed: 5 additions & 4 deletions
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‎

Lines changed: 5 additions & 5 deletions
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‎

Lines changed: 4 additions & 2 deletions
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

‎stubs/paramiko/paramiko/proxy.pyi‎

Lines changed: 2 additions & 1 deletion
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

0 commit comments

Comments
 (0)