From 9807a61f6c1f5ae58af4625ea682e1e127cf9a65 Mon Sep 17 00:00:00 2001 From: Ron Frederick Date: Sat, 7 Jan 2023 20:38:17 -0800 Subject: [PATCH] Update for errors detected by mypy 0.991 This commit addresses new errors and warnings reported by mypy 0.991. In particular, by default mypy no longer allows Optional to be implicit when declaring some other type but defaulting the value to None. --- asyncssh/agent.py | 12 +++--- asyncssh/channel.py | 16 +++---- asyncssh/connection.py | 17 +++----- asyncssh/crypto/dsa.py | 5 ++- asyncssh/crypto/ed.py | 7 +-- asyncssh/crypto/misc.py | 8 +++- asyncssh/crypto/rsa.py | 4 +- asyncssh/forward.py | 4 +- asyncssh/gss.py | 7 ++- asyncssh/gss_win32.py | 4 +- asyncssh/pkcs11.py | 24 +++++------ asyncssh/process.py | 4 +- asyncssh/public_key.py | 94 ++++++++++++++++++++++------------------- asyncssh/saslprep.py | 10 +++-- asyncssh/sftp.py | 8 ++-- asyncssh/sk_ecdsa.py | 4 +- asyncssh/sk_eddsa.py | 4 +- asyncssh/socks.py | 7 +-- asyncssh/subprocess.py | 6 +-- asyncssh/x11.py | 4 +- 20 files changed, 135 insertions(+), 114 deletions(-) diff --git a/asyncssh/agent.py b/asyncssh/agent.py index ef29cde4..a7762833 100644 --- a/asyncssh/agent.py +++ b/asyncssh/agent.py @@ -1,4 +1,4 @@ -# Copyright (c) 2016-2022 by Ron Frederick and others. +# Copyright (c) 2016-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -314,7 +314,8 @@ async def sign(self, key_blob: bytes, data: bytes, raise ValueError('Unknown SSH agent response: %d' % resptype) async def add_keys(self, keylist: KeyPairListArg = (), - passphrase: str = None, lifetime: int = None, + passphrase: Optional[str] = None, + lifetime: Optional[int] = None, confirm: bool = False) -> None: """Add keys to the agent @@ -394,8 +395,9 @@ async def add_keys(self, keylist: KeyPairListArg = (), else: raise ValueError('Unknown SSH agent response: %d' % resptype) - async def add_smartcard_keys(self, provider: str, pin: str = None, - lifetime: int = None, + async def add_smartcard_keys(self, provider: str, + pin: Optional[str] = None, + lifetime: Optional[int] = None, confirm: bool = False) -> None: """Store keys associated with a smart card in the agent @@ -458,7 +460,7 @@ async def remove_keys(self, keylist: Sequence[SSHKeyPair]) -> None: raise ValueError('Unknown SSH agent response: %d' % resptype) async def remove_smartcard_keys(self, provider: str, - pin: str = None) -> None: + pin: Optional[str] = None) -> None: """Remove keys associated with a smart card stored in the agent :param provider: diff --git a/asyncssh/channel.py b/asyncssh/channel.py index 704a1584..7f945c96 100644 --- a/asyncssh/channel.py +++ b/asyncssh/channel.py @@ -1,4 +1,4 @@ -# Copyright (c) 2013-2022 by Ron Frederick and others. +# Copyright (c) 2013-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -75,7 +75,7 @@ _signal_names = {v: k for (k, v) in _signal_numbers.items()} _ExitSignal = Tuple[str, bool, str, str] -_RequestHandler = Callable[[SSHPacket], Optional[bool]] +_RequestHandler = Optional[Callable[[SSHPacket], Optional[bool]]] class SSHChannel(Generic[AnyStr], SSHPacketHandler): @@ -198,7 +198,7 @@ def get_write_datatypes(self) -> Set[int]: return self._write_datatypes - def _cleanup(self, exc: Exception = None) -> None: + def _cleanup(self, exc: Optional[Exception] = None) -> None: """Clean up this channel""" if self._open_waiter: @@ -322,7 +322,7 @@ def _flush_send_buf(self) -> None: elif self._send_state == 'close_pending': self._close_send() - def _flush_recv_buf(self, exc: Exception = None) -> None: + def _flush_recv_buf(self, exc: Optional[Exception] = None) -> None: """Flush as much data in the recv buffer as the application allows""" while self._recv_buf and not self._recv_paused: @@ -847,8 +847,8 @@ def get_write_buffer_size(self) -> int: return self._send_buf_len - def set_write_buffer_limits(self, high: int = None, - low: int = None) -> None: + def set_write_buffer_limits(self, high: Optional[int] = None, + low: Optional[int] = None) -> None: """Set the high- and low-water limits for write flow control This method sets the limits used when deciding when to call @@ -1623,8 +1623,8 @@ def _process_env_request(self, packet: SSHPacket) -> bool: self._env[name] = value return True - def _start_session(self, command: str = None, - subsystem: str = None) -> bool: + def _start_session(self, command: Optional[str] = None, + subsystem: Optional[str] = None) -> bool: """Tell the session what type of channel is being requested""" forced_command = \ diff --git a/asyncssh/connection.py b/asyncssh/connection.py index c3beae05..898652b9 100644 --- a/asyncssh/connection.py +++ b/asyncssh/connection.py @@ -1,4 +1,4 @@ -# Copyright (c) 2013-2022 by Ron Frederick and others. +# Copyright (c) 2013-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -283,12 +283,6 @@ def __init__(self) -> None: self._conn = conn_factory() self._close_event = asyncio.Event() - def set_protocol(self, protocol: asyncio.BaseProtocol) -> None: - """Changing the protocol is ignored here""" - - def get_protocol(self) -> asyncio.BaseProtocol: - """Changing the protocol is ignored here""" - def get_extra_info(self, name: str, default: Any = None) -> Any: """Return extra information associated with this tunnel""" @@ -1053,7 +1047,8 @@ def _reap_task(self, task_logger: SSHLogger, self.internal_error(error_logger=task_logger) def create_task(self, coro: Awaitable[None], - task_logger: SSHLogger = None) -> 'asyncio.Task[None]': + task_logger: Optional[SSHLogger] = None) -> \ + 'asyncio.Task[None]': """Create an asynchronous task which catches and reports errors""" task = asyncio.ensure_future(coro) @@ -1543,7 +1538,7 @@ def _recv_packet(self) -> bool: return True def send_packet(self, pkttype: int, *args: bytes, - handler: SSHPacketLogger = None) -> None: + handler: Optional[SSHPacketLogger] = None) -> None: """Send an SSH packet""" if (self._auth_complete and self._kex_complete and @@ -1825,7 +1820,7 @@ def get_userauth_request_data(self, method: bytes, *args: bytes) -> bytes: self._get_userauth_request_packet(method, args)) def send_userauth_packet(self, pkttype: int, *args: bytes, - handler: SSHPacketLogger = None, + handler: Optional[SSHPacketLogger] = None, trivial: bool = True) -> None: """Send a user authentication packet""" @@ -7079,7 +7074,7 @@ def prepare(self, last_config: Optional[SSHConfig] = None, # type: ignore server_host_key_algs: _AlgsArg = (), username: DefTuple[str] = (), password: Optional[str] = None, client_host_keysign: DefTuple[KeySignPath] = (), - client_host_keys: _ClientKeysArg = None, + client_host_keys: Optional[_ClientKeysArg] = None, client_host_certs: Sequence[FilePath] = (), client_host: Optional[str] = None, client_username: DefTuple[str] = (), diff --git a/asyncssh/crypto/dsa.py b/asyncssh/crypto/dsa.py index 8f3bd32b..befb5441 100644 --- a/asyncssh/crypto/dsa.py +++ b/asyncssh/crypto/dsa.py @@ -1,4 +1,4 @@ -# Copyright (c) 2014-2021 by Ron Frederick and others. +# Copyright (c) 2014-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -36,7 +36,8 @@ class _DSAKey(CryptoKey): """Base class for shim around PyCA for DSA keys""" def __init__(self, pyca_key: PyCAKey, params: dsa.DSAParameterNumbers, - pub: dsa.DSAPublicNumbers, priv: dsa.DSAPrivateNumbers = None): + pub: dsa.DSAPublicNumbers, + priv: Optional[dsa.DSAPrivateNumbers] = None): super().__init__(pyca_key) self._params = params diff --git a/asyncssh/crypto/ed.py b/asyncssh/crypto/ed.py index ea9a5965..1b702eb8 100644 --- a/asyncssh/crypto/ed.py +++ b/asyncssh/crypto/ed.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2021 by Ron Frederick and others. +# Copyright (c) 2019-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -50,7 +50,8 @@ class _EdDSAKey(CryptoKey): """Base class for shim around PyCA for EdDSA keys""" - def __init__(self, pyca_key: PyCAKey, pub: bytes, priv: bytes = None): + def __init__(self, pyca_key: PyCAKey, pub: bytes, + priv: Optional[bytes] = None): super().__init__(pyca_key) self._pub = pub @@ -146,7 +147,7 @@ def verify(self, data: bytes, sig: bytes, hash_name: str = '') -> bool: class _EdDSANaclKey: """Base class for shim around libnacl for EdDSA keys""" - def __init__(self, pub: bytes, priv: bytes = None): + def __init__(self, pub: bytes, priv: Optional[bytes] = None): self._pub = pub self._priv = priv diff --git a/asyncssh/crypto/misc.py b/asyncssh/crypto/misc.py index 1cc54557..60c4e052 100644 --- a/asyncssh/crypto/misc.py +++ b/asyncssh/crypto/misc.py @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2021 by Ron Frederick and others. +# Copyright (c) 2017-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -60,5 +60,11 @@ def pyca_key(self) -> PyCAKey: def sign(self, data: bytes, hash_name: str = '') -> bytes: """Sign a block of data""" + # pylint: disable=no-self-use + raise RuntimeError + def verify(self, data: bytes, sig: bytes, hash_name: str = '') -> bool: """Verify the signature on a block of data""" + + # pylint: disable=no-self-use + raise RuntimeError diff --git a/asyncssh/crypto/rsa.py b/asyncssh/crypto/rsa.py index 989666ac..a62f43ea 100644 --- a/asyncssh/crypto/rsa.py +++ b/asyncssh/crypto/rsa.py @@ -1,4 +1,4 @@ -# Copyright (c) 2014-2021 by Ron Frederick and others. +# Copyright (c) 2014-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -38,7 +38,7 @@ class _RSAKey(CryptoKey): """Base class for shim around PyCA for RSA keys""" def __init__(self, pyca_key: PyCAKey, pub: rsa.RSAPublicNumbers, - priv: rsa.RSAPrivateNumbers = None): + priv: Optional[rsa.RSAPrivateNumbers] = None): super().__init__(pyca_key) self._pub = pub diff --git a/asyncssh/forward.py b/asyncssh/forward.py index 784db1d3..b3896c0f 100644 --- a/asyncssh/forward.py +++ b/asyncssh/forward.py @@ -1,4 +1,4 @@ -# Copyright (c) 2013-2021 by Ron Frederick and others. +# Copyright (c) 2013-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -106,7 +106,7 @@ def session_started(self) -> None: """Handle session start""" def data_received(self, data: bytes, - datatype: int = None) -> None: + datatype: Optional[int] = None) -> None: """Handle incoming data from the transport""" # pylint: disable=unused-argument diff --git a/asyncssh/gss.py b/asyncssh/gss.py index 8039bcda..86e92e17 100644 --- a/asyncssh/gss.py +++ b/asyncssh/gss.py @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2021 by Ron Frederick and others. +# Copyright (c) 2017-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -22,6 +22,8 @@ import sys +from typing import Optional + try: # pylint: disable=unused-import @@ -37,7 +39,8 @@ class GSSError(ValueError): # type: ignore """Stub class for reporting that GSS is not available""" - def __init__(self, maj_code: int, min_code: int, token: bytes = None): + def __init__(self, maj_code: int, min_code: int, + token: Optional[bytes] = None): super().__init__('GSS not available') self.maj_code = maj_code diff --git a/asyncssh/gss_win32.py b/asyncssh/gss_win32.py index f9a6897f..8c68ba4e 100644 --- a/asyncssh/gss_win32.py +++ b/asyncssh/gss_win32.py @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2022 by Ron Frederick and others. +# Copyright (c) 2017-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -194,7 +194,7 @@ class GSSError(Exception): """Class for reporting GSS errors""" def __init__(self, maj_code: int = 0, min_code: int = 0, - token: bytes = None, details: str = ''): + token: Optional[bytes] = None, details: str = ''): super().__init__(details) self.maj_code = maj_code diff --git a/asyncssh/pkcs11.py b/asyncssh/pkcs11.py index 06049532..d907b497 100644 --- a/asyncssh/pkcs11.py +++ b/asyncssh/pkcs11.py @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2021 by Ron Frederick and others. +# Copyright (c) 2020-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -68,7 +68,7 @@ class SSHPKCS11KeyPair(SSHKeyPair): _key_type = 'pkcs11' def __init__(self, session: 'SSHPKCS11Session', privkey: PrivateKey, - pubkey: SSHKey, cert: SSHCertificate = None): + pubkey: SSHKey, cert: Optional[SSHCertificate] = None): super().__init__(pubkey.algorithm, pubkey.algorithm, pubkey.sig_algorithms, pubkey.sig_algorithms, pubkey.public_data, privkey.label, cert, @@ -197,12 +197,12 @@ def get_keys(self, load_certs: bool, key_label: Optional[str], return keys - def load_pkcs11_keys(provider: str, pin: str = None, *, + def load_pkcs11_keys(provider: str, pin: Optional[str] = None, *, load_certs: bool = True, - token_label: str = None, - token_serial: BytesOrStr = None, - key_label: str = None, - key_id: BytesOrStr = None) -> \ + token_label: Optional[str] = None, + token_serial: Optional[BytesOrStr] = None, + key_label: Optional[str] = None, + key_id: Optional[BytesOrStr] = None) -> \ Sequence[SSHPKCS11KeyPair]: """Load PIV keys and X.509 certificates from a PKCS#11 token @@ -278,12 +278,12 @@ def load_pkcs11_keys(provider: str, pin: str = None, *, return keys else: # pragma: no cover - def load_pkcs11_keys(provider: str, pin: str = None, *, + def load_pkcs11_keys(provider: str, pin: Optional[str] = None, *, load_certs: bool = True, - token_label: str = None, - token_serial: BytesOrStr = None, - key_label: str = None, - key_id: BytesOrStr = None) -> \ + token_label: Optional[str] = None, + token_serial: Optional[BytesOrStr] = None, + key_label: Optional[str] = None, + key_id: Optional[BytesOrStr] = None) -> \ Sequence['SSHPKCS11KeyPair']: """Report that PKCS#11 support is not available""" diff --git a/asyncssh/process.py b/asyncssh/process.py index 939dc02e..40cef998 100644 --- a/asyncssh/process.py +++ b/asyncssh/process.py @@ -1,4 +1,4 @@ -# Copyright (c) 2016-2021 by Ron Frederick and others. +# Copyright (c) 2016-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -101,6 +101,8 @@ def write(self, data: _AnyStrContra) -> None: def write_exception(self, exc: Exception) -> None: """Write exception (break, signal, terminal size change)""" + return + def write_eof(self) -> None: """Close output when end of file is received""" diff --git a/asyncssh/public_key.py b/asyncssh/public_key.py index e291c48d..75672ed4 100644 --- a/asyncssh/public_key.py +++ b/asyncssh/public_key.py @@ -1,4 +1,4 @@ -# Copyright (c) 2013-2022 by Ron Frederick and others. +# Copyright (c) 2013-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -633,22 +633,18 @@ def convert_to_public(self) -> 'SSHKey': result.set_filename(self._filename) return result - def generate_user_certificate(self, user_key: 'SSHKey', key_id: str, - version: int = 1, serial: int = 0, - principals: _CertPrincipals = (), - valid_after: _Time = 0, - valid_before: _Time = 0xffffffffffffffff, - force_command: str = None, - source_address: Sequence[str] = None, - permit_x11_forwarding: bool = True, - permit_agent_forwarding: bool = True, - permit_port_forwarding: bool = True, - permit_pty: bool = True, - permit_user_rc: bool = True, - touch_required: bool = True, - sig_alg: DefTuple[str] = (), - comment: DefTuple[_Comment] = ()) -> \ - 'SSHOpenSSHCertificate': + def generate_user_certificate( + self, user_key: 'SSHKey', key_id: str, version: int = 1, + serial: int = 0, principals: _CertPrincipals = (), + valid_after: _Time = 0, valid_before: _Time = 0xffffffffffffffff, + force_command: Optional[str] = None, + source_address: Optional[Sequence[str]] = None, + permit_x11_forwarding: bool = True, + permit_agent_forwarding: bool = True, + permit_port_forwarding: bool = True, permit_pty: bool = True, + permit_user_rc: bool = True, touch_required: bool = True, + sig_alg: DefTuple[str] = (), + comment: DefTuple[_Comment] = ()) -> 'SSHOpenSSHCertificate': """Generate a new SSH user certificate This method returns an SSH user certifcate with the requested @@ -821,16 +817,14 @@ def generate_host_certificate(self, host_key: 'SSHKey', key_id: str, principals, valid_after, valid_before, {}, sig_alg, comment) - def generate_x509_user_certificate(self, user_key: 'SSHKey', subject: str, - issuer: str = None, serial: int = None, - principals: _CertPrincipals = (), - valid_after: _Time = 0, - valid_before: _Time = 0xffffffffffffffff, - purposes: X509CertPurposes = \ - 'secureShellClient', - hash_alg: DefTuple[str] = (), - comment: DefTuple[_Comment] = ()) -> \ - 'SSHX509Certificate': + def generate_x509_user_certificate( + self, user_key: 'SSHKey', subject: str, + issuer: Optional[str] = None, serial: Optional[int] = None, + principals: _CertPrincipals = (), valid_after: _Time = 0, + valid_before: _Time = 0xffffffffffffffff, + purposes: X509CertPurposes = 'secureShellClient', + hash_alg: DefTuple[str] = (), + comment: DefTuple[_Comment] = ()) -> 'SSHX509Certificate': """Generate a new X.509 user certificate This method returns an X.509 user certifcate with the requested @@ -894,16 +888,14 @@ def generate_x509_user_certificate(self, user_key: 'SSHKey', subject: str, purposes, principals, (), hash_alg, comment) - def generate_x509_host_certificate(self, host_key: 'SSHKey', subject: str, - issuer: str = None, serial: int = None, - principals: _CertPrincipals = (), - valid_after: _Time = 0, - valid_before: _Time = 0xffffffffffffffff, - purposes: X509CertPurposes = \ - 'secureShellServer', - hash_alg: DefTuple[str] = (), - comment: DefTuple[_Comment] = ()) -> \ - 'SSHX509Certificate': + def generate_x509_host_certificate( + self, host_key: 'SSHKey', subject: str, + issuer: Optional[str] = None, serial: Optional[int] = None, + principals: _CertPrincipals = (), valid_after: _Time = 0, + valid_before: _Time = 0xffffffffffffffff, + purposes: X509CertPurposes = 'secureShellServer', + hash_alg: DefTuple[str] = (), + comment: DefTuple[_Comment] = ()) -> 'SSHX509Certificate': """Generate a new X.509 host certificate This method returns a X.509 host certifcate with the requested @@ -1376,6 +1368,8 @@ def construct(cls, packet: SSHPacket, algorithm: bytes, comment: _Comment) -> 'SSHCertificate': """Construct an SSH certificate from packetized data""" + raise NotImplementedError + def __eq__(self, other: object) -> bool: return (isinstance(other, type(self)) and self.public_data == other.public_data) @@ -1617,7 +1611,7 @@ def generate(cls, signing_key: 'SSHKey', algorithm: bytes, key: 'SSHKey', @classmethod def construct(cls, packet: SSHPacket, algorithm: bytes, key_handler: Optional[Type[SSHKey]], - comment: _Comment) -> 'SSHCertificate': + comment: _Comment) -> 'SSHOpenSSHCertificate': """Construct an SSH certificate from packetized data""" assert key_handler is not None @@ -1891,6 +1885,14 @@ def _expand_trust_store(self, cert: 'SSHX509Certificate', except (OSError, KeyImportError): pass + @classmethod + def construct(cls, packet: SSHPacket, algorithm: bytes, + key_handler: Optional[Type[SSHKey]], + comment: _Comment) -> 'SSHX509Certificate': + """Construct an SSH X.509 certificate from packetized data""" + + raise RuntimeError + @classmethod def generate(cls, signing_key: 'SSHKey', key: 'SSHKey', subject: str, issuer: Optional[str], serial: Optional[int], @@ -2047,7 +2049,8 @@ def __init__(self, algorithm: bytes, sig_algorithm: bytes, sig_algorithms: Sequence[bytes], host_key_algorithms: Sequence[bytes], public_data: bytes, comment: _Comment, - cert: SSHCertificate = None, filename: bytes = None, + cert: Optional[SSHCertificate] = None, + filename: Optional[bytes] = None, use_executor: bool = False): self.key_algorithm = algorithm self.key_public_data = public_data @@ -2198,6 +2201,9 @@ def set_sig_algorithm(self, sig_algorithm: bytes) -> None: def sign(self, data: bytes) -> bytes: """Sign a block of data with this private key""" + # pylint: disable=no-self-use + raise RuntimeError + class SSHLocalKeyPair(SSHKeyPair): """Class which holds a local asymmetric key pair @@ -2210,8 +2216,8 @@ class SSHLocalKeyPair(SSHKeyPair): _key_type = 'local' - def __init__(self, key: SSHKey, pubkey: SSHKey = None, - cert: SSHCertificate = None): + def __init__(self, key: SSHKey, pubkey: Optional[SSHKey] = None, + cert: Optional[SSHCertificate] = None): if pubkey and pubkey.public_data != key.public_data: raise ValueError('Public key mismatch') @@ -2883,7 +2889,8 @@ def register_sk_alg(sk_alg: int, handler: Type[SSHKey], *args: object) -> None: def register_public_key_alg(algorithm: bytes, handler: Type[SSHKey], default: bool, - sig_algorithms: Sequence[bytes] = None) -> None: + sig_algorithms: Optional[Sequence[bytes]] = \ + None) -> None: """Register a new public key algorithm""" if not sig_algorithms: @@ -3717,7 +3724,8 @@ def load_default_identities() -> Sequence[bytes]: return result -def load_resident_keys(pin: str, *, application: str = 'ssh:', user: str = None, +def load_resident_keys(pin: str, *, application: str = 'ssh:', + user: Optional[str] = None, touch_required: bool = True) -> Sequence[SSHKey]: """Load keys resident on attached FIDO2 security keys diff --git a/asyncssh/saslprep.py b/asyncssh/saslprep.py index fe6a2cbf..569e5029 100644 --- a/asyncssh/saslprep.py +++ b/asyncssh/saslprep.py @@ -1,4 +1,4 @@ -# Copyright (c) 2013-2021 by Ron Frederick and others. +# Copyright (c) 2013-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -31,7 +31,7 @@ # pylint: disable=deprecated-module import stringprep # pylint: enable=deprecated-module -from typing import Callable, Sequence +from typing import Callable, Optional, Sequence import unicodedata @@ -60,8 +60,10 @@ def _check_bidi(s: str) -> None: raise SASLPrepError('RandALCat character not at both start and end') -def _stringprep(s: str, check_unassigned: bool, mapping: Callable[[str], str], - normalization: str, prohibited: Sequence[Callable[[str], bool]], +def _stringprep(s: str, check_unassigned: bool, + mapping: Optional[Callable[[str], str]], + normalization: str, + prohibited: Sequence[Callable[[str], bool]], bidi: bool) -> str: """Implement a stringprep profile as defined in RFC 3454""" diff --git a/asyncssh/sftp.py b/asyncssh/sftp.py index 0c05b5d4..bb1aea82 100644 --- a/asyncssh/sftp.py +++ b/asyncssh/sftp.py @@ -1,4 +1,4 @@ -# Copyright (c) 2015-2022 by Ron Frederick and others. +# Copyright (c) 2015-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -141,9 +141,9 @@ _SFTPOSAttrs = Union[os.stat_result, 'SFTPAttrs'] _SFTPOSVFSAttrs = Union[os.statvfs_result, 'SFTPVFSAttrs'] -_SFTPOnErrorHandler = Callable[[Callable, bytes, OptExcInfo], None] -_SFTPPacketHandler = Callable[['SFTPServerHandler', SSHPacket], - Awaitable[object]] +_SFTPOnErrorHandler = Optional[Callable[[Callable, bytes, OptExcInfo], None]] +_SFTPPacketHandler = Optional[Callable[['SFTPServerHandler', SSHPacket], + Awaitable[object]]] SFTPErrorHandler = Union[None, Literal[False], Callable[[Exception], None]] SFTPProgressHandler = Optional[Callable[[bytes, bytes, int, int], None]] diff --git a/asyncssh/sk_ecdsa.py b/asyncssh/sk_ecdsa.py index b8c3854a..107de5b7 100644 --- a/asyncssh/sk_ecdsa.py +++ b/asyncssh/sk_ecdsa.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2021 by Ron Frederick and others. +# Copyright (c) 2019-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -45,7 +45,7 @@ class _SKECDSAKey(SSHKey): def __init__(self, curve_id: bytes, public_value: bytes, application: bytes, flags: int = 0, - key_handle: bytes = None, reserved: bytes = b''): + key_handle: Optional[bytes] = None, reserved: bytes = b''): super().__init__(ECDSAPublicKey.construct(curve_id, public_value)) self.algorithm = b'sk-ecdsa-sha2-' + curve_id + b'@openssh.com' diff --git a/asyncssh/sk_eddsa.py b/asyncssh/sk_eddsa.py index 94ea4d79..7ca8da70 100644 --- a/asyncssh/sk_eddsa.py +++ b/asyncssh/sk_eddsa.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2021 by Ron Frederick and others. +# Copyright (c) 2019-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -46,7 +46,7 @@ class _SKEd25519Key(SSHKey): use_executor = True def __init__(self, public_value: bytes, application: bytes, - flags: int = 0, key_handle: bytes = None, + flags: int = 0, key_handle: Optional[bytes] = None, reserved: bytes = b''): super().__init__(EdDSAPublicKey.construct(b'ed25519', public_value)) diff --git a/asyncssh/socks.py b/asyncssh/socks.py index cb818d81..960cda60 100644 --- a/asyncssh/socks.py +++ b/asyncssh/socks.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018-2021 by Ron Frederick and others. +# Copyright (c) 2018-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -24,6 +24,7 @@ from typing import TYPE_CHECKING, Callable, Optional from .forward import SSHForwarderCoro, SSHLocalForwarder +from .session import DataType if TYPE_CHECKING: @@ -212,13 +213,13 @@ def _recv_socks5_port(self, data: bytes) -> None: self._send_socks5_ok() self._connect() - def data_received(self, data: bytes, datatype: int = None) -> None: + def data_received(self, data: bytes, datatype: DataType = None) -> None: """Handle incoming data from the SOCKS client""" if self._recv_handler: self._inpbuf += data - while self._recv_handler: + while self._recv_handler: # type: ignore[truthy-function] if self._bytes_needed < 0: idx = self._inpbuf.find(b'\0') if idx >= 0: diff --git a/asyncssh/subprocess.py b/asyncssh/subprocess.py index 7655e0bd..f1a9816b 100644 --- a/asyncssh/subprocess.py +++ b/asyncssh/subprocess.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2021 by Ron Frederick and others. +# Copyright (c) 2019-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -94,8 +94,8 @@ def get_write_buffer_size(self) -> int: return self._chan.get_write_buffer_size() - def set_write_buffer_limits(self, high: int = None, - low: int = None) -> None: + def set_write_buffer_limits(self, high: Optional[int] = None, + low: Optional[int] = None) -> None: """Set the high- and low-water limits for write flow control""" self._chan.set_write_buffer_limits(high, low) diff --git a/asyncssh/x11.py b/asyncssh/x11.py index 7765b64b..0c7a808a 100644 --- a/asyncssh/x11.py +++ b/asyncssh/x11.py @@ -1,4 +1,4 @@ -# Copyright (c) 2016-2021 by Ron Frederick and others. +# Copyright (c) 2016-2023 by Ron Frederick and others. # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License v2.0 which accompanies this @@ -242,7 +242,7 @@ def data_received(self, data: bytes, datatype: DataType = None) -> None: if self._recv_handler: self._inpbuf += data - while self._recv_handler: + while self._recv_handler: # type: ignore[truthy-function] if len(self._inpbuf) >= self._bytes_needed: data = self._inpbuf[:self._bytes_needed] self._inpbuf = self._inpbuf[self._bytes_needed:]