Skip to content

Commit

Permalink
Reverse mypy exceptions
Browse files Browse the repository at this point in the history
Instead of disabling strict checks per default and only enabling them
for new code, this patch enables strict checks per default and disables
them for old code.  This makes sure that new code is not accidentally
ignored.

This patch also fixes some simple typing issues so that we can drop some
exceptions.
  • Loading branch information
robin-nitrokey committed Nov 14, 2023
1 parent 815bb68 commit 7ff231f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 29 deletions.
8 changes: 5 additions & 3 deletions pynitrokey/cli/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
# http://opensource.org/licenses/MIT>, at your option. This file may not be
# copied, modified, or distributed except according to those terms.

from typing import Any

from pynitrokey.helpers import local_critical


class CliException(Exception):
def __init__(
self, *messages, support_hint: bool = True, ret_code: int = 1, **kwargs
):
self, *messages: Any, support_hint: bool = True, ret_code: int = 1, **kwargs: Any
) -> None:
super().__init__("\n".join([str(message) for message in messages]))

self.messages = messages
self.support_hint = support_hint
self.ret_code = ret_code
self.kwargs = kwargs

def show(self):
def show(self) -> None:
local_critical(
*self.messages,
support_hint=self.support_hint,
Expand Down
3 changes: 2 additions & 1 deletion pynitrokey/cli/fido2.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
from fido2.client import ClientError as Fido2ClientError
from fido2.ctap import CtapError
from fido2.ctap1 import ApduError
from fido2.ctap2 import CredentialManagement, Ctap2
from fido2.ctap2.base import Ctap2
from fido2.ctap2.credman import CredentialManagement
from fido2.ctap2.pin import ClientPin, PinProtocol
from fido2.hid import CtapHidDevice

Expand Down
4 changes: 2 additions & 2 deletions pynitrokey/cli/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

@click.command()
@click.argument("serial_port")
def monitor(serial_port):
def monitor(serial_port: str) -> None:
"""Reads Nitrokey serial output from USB serial port SERIAL_PORT.
SERIAL-PORT is something like /dev/ttyACM0 or COM10.
Expand All @@ -36,7 +36,7 @@ def monitor(serial_port):
sys.stdout.flush()
time.sleep(0.5)

def reconnect():
def reconnect() -> serial.Serial:
while True:
time.sleep(0.5)
try:
Expand Down
2 changes: 1 addition & 1 deletion pynitrokey/fido2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from fido2.cose import ES256, EdDSA
from fido2.ctap import CtapError
from fido2.ctap1 import Ctap1
from fido2.ctap2 import CredentialManagement, Ctap2
from fido2.ctap2.base import Ctap2
from fido2.ctap2.credman import CredentialManagement
from fido2.ctap2.pin import ClientPin
from fido2.hid import CTAPHID, CtapHidDevice, open_device
Expand Down
2 changes: 1 addition & 1 deletion pynitrokey/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ProgressBar:
"""

def __init__(self, **kwargs: Any) -> None:
self.bar: Optional[tqdm] = None
self.bar: Optional[tqdm[Any]] = None
self.kwargs = kwargs
self.sum = 0

Expand Down
2 changes: 1 addition & 1 deletion pynitrokey/nk3/bootloader/lpc55.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import List, Optional, Tuple

from spsdk.mboot import McuBoot, StatusCode
from spsdk.mboot.interfaces import RawHid
from spsdk.mboot.interfaces.usb import RawHid
from spsdk.mboot.properties import PropertyTag
from spsdk.sbfile.sb2.images import BootImageV21
from spsdk.utils.usbfilter import USBDeviceFilter
Expand Down
45 changes: 25 additions & 20 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,37 @@ profile = "black"
mypy_path = "stubs"
show_error_codes = true
python_version = "3.9"
warn_unused_configs = true
warn_redundant_casts = true
strict = true

# enable strict checks for new code, see
# disable strict checks for old code, see
# - https://github.com/python/mypy/issues/11401
# - https://mypy.readthedocs.io/en/stable/existing_code.html#introduce-stricter-options
[[tool.mypy.overrides]]
module = [
"pynitrokey.cli.fido2.*",
"pynitrokey.cli.nk3.*",
"pynitrokey.fido2.*",
"pynitrokey.nk3.*",
"pynitrokey.updates.*",
"pynitrokey.cli",
"pynitrokey.cli.nethsm",
"pynitrokey.cli.pro",
"pynitrokey.cli.program",
"pynitrokey.cli.start",
"pynitrokey.cli.storage",
"pynitrokey.cli.update",
"pynitrokey.conftest",
"pynitrokey.libnk",
"pynitrokey.start.*",
"pynitrokey.test_secrets_app",
]
check_untyped_defs = true
disallow_any_generics = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
no_implicit_reexport = true
strict_concatenate = true
strict_equality = true
warn_unused_ignores = true
warn_return_any = true
check_untyped_defs = false
disallow_any_generics = false
disallow_incomplete_defs = false
disallow_subclassing_any = false
disallow_untyped_calls = false
disallow_untyped_decorators = false
disallow_untyped_defs = false
no_implicit_reexport = false
strict_concatenate = false
strict_equality = false
warn_unused_ignores = false
warn_return_any = false


# pynitrokey.nk3.bootloader.nrf52_upload is only temporary in this package
Expand Down

0 comments on commit 7ff231f

Please sign in to comment.