Skip to content

Commit

Permalink
Update for errors detected by mypy 0.991
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ronf committed Jan 8, 2023
1 parent ed5eef8 commit 9807a61
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 114 deletions.
12 changes: 7 additions & 5 deletions asyncssh/agent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2016-2022 by Ron Frederick <ronf@timeheart.net> and others.
# Copyright (c) 2016-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 8 additions & 8 deletions asyncssh/channel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2013-2022 by Ron Frederick <ronf@timeheart.net> and others.
# Copyright (c) 2013-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = \
Expand Down
17 changes: 6 additions & 11 deletions asyncssh/connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2013-2022 by Ron Frederick <ronf@timeheart.net> and others.
# Copyright (c) 2013-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
Expand Down Expand Up @@ -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"""

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"""

Expand Down Expand Up @@ -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] = (),
Expand Down
5 changes: 3 additions & 2 deletions asyncssh/crypto/dsa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2014-2021 by Ron Frederick <ronf@timeheart.net> and others.
# Copyright (c) 2014-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions asyncssh/crypto/ed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2021 by Ron Frederick <ronf@timeheart.net> and others.
# Copyright (c) 2019-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion asyncssh/crypto/misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017-2021 by Ron Frederick <ronf@timeheart.net> and others.
# Copyright (c) 2017-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions asyncssh/crypto/rsa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2014-2021 by Ron Frederick <ronf@timeheart.net> and others.
# Copyright (c) 2014-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions asyncssh/forward.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2013-2021 by Ron Frederick <ronf@timeheart.net> and others.
# Copyright (c) 2013-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions asyncssh/gss.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017-2021 by Ron Frederick <ronf@timeheart.net> and others.
# Copyright (c) 2017-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
Expand All @@ -22,6 +22,8 @@

import sys

from typing import Optional

try:
# pylint: disable=unused-import

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions asyncssh/gss_win32.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017-2022 by Ron Frederick <ronf@timeheart.net> and others.
# Copyright (c) 2017-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
Expand Down Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions asyncssh/pkcs11.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2021 by Ron Frederick <ronf@timeheart.net> and others.
# Copyright (c) 2020-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"""

Expand Down
4 changes: 3 additions & 1 deletion asyncssh/process.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2016-2021 by Ron Frederick <ronf@timeheart.net> and others.
# Copyright (c) 2016-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
Expand Down Expand Up @@ -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"""

Expand Down
Loading

0 comments on commit 9807a61

Please sign in to comment.