Skip to content

Commit

Permalink
Add missing stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-nitrokey committed Jan 3, 2025
1 parent 3b78572 commit a55974c
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 31 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ builde:

# code checks
check-format:
$(PYTHON3_VENV) -m black --check $(PACKAGE_NAME)/
$(PYTHON3_VENV) -m black --check $(PACKAGE_NAME)/ stubs

check-import-sorting:
$(PYTHON3_VENV) -m isort --check-only $(PACKAGE_NAME)/
$(PYTHON3_VENV) -m isort --check-only $(PACKAGE_NAME)/ stubs

check-style:
$(PYTHON3_VENV) -m flake8 $(FLAKE8_DIRS)
$(PYTHON3_VENV) -m flake8 $(FLAKE8_DIRS) stubs

check-typing:
@echo "Note: run semi-clean target in case this fails without any proper reason"
Expand All @@ -56,8 +56,8 @@ check: check-format check-import-sorting check-style check-typing

# automatic code fixes
fix:
$(PYTHON3_VENV) -m black $(BLACK_FLAGS) $(PACKAGE_NAME)/
$(PYTHON3_VENV) -m isort $(ISORT_FLAGS) $(PACKAGE_NAME)/
$(PYTHON3_VENV) -m black $(BLACK_FLAGS) $(PACKAGE_NAME)/ stubs
$(PYTHON3_VENV) -m isort $(ISORT_FLAGS) $(PACKAGE_NAME)/ stubs

semi-clean:
rm -rf ./**/__pycache__
Expand Down
2 changes: 1 addition & 1 deletion pynitrokey/cli/pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def update(firmware_path: str):
dfu_device = None
try:
dfu_device = dfu.DFU(device.open())
except usb1.USBErrorAccess as e:
except usb1.USBErrorAccess as e: # type: ignore[attr-defined]
print(f"Cannot connect to the device: {device} -> {e}")
if "LIBUSB_ERROR_ACCESS" in str(e) and platform.system().lower() == "linux":
print(
Expand Down
2 changes: 1 addition & 1 deletion pynitrokey/fido2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def parseField(f: str) -> bytes:
s = i
ext = min(i + chunk, seg[1])
data = ih.tobinarray(start=i, size=ext - s)
self.write_flash(i, data)
self.write_flash(i, data) # type: ignore[arg-type]
total += chunk
progress = total / float(size) * 100
sys.stdout.write("updating firmware %.2f%%...\r" % progress)
Expand Down
7 changes: 5 additions & 2 deletions pynitrokey/fido2/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def genkey(
output_pem_file: str, input_seed_file: Optional[str] = None
) -> ecdsa.VerifyingKey:
from ecdsa import NIST256p, SigningKey
from ecdsa.util import randrange_from_seed__trytryagain

# TODO: it looks like this is an internal API -- do we really want to use it?
# If yes, we should add type annotations.
from ecdsa.util import randrange_from_seed__trytryagain # type: ignore[import]

if input_seed_file is not None:
seed = input_seed_file
Expand Down Expand Up @@ -238,7 +241,7 @@ def sign_firmware_for_version(
print("im_size: ", im_size)
print("firmware_size: ", len(arr))

byts = (arr).tobytes() if hasattr(arr, "tobytes") else (arr).tostring()
byts = (arr).tobytes() if hasattr(arr, "tobytes") else (arr).tostring() # type: ignore[attr-defined]
h = sha256()
h.update(byts)
sig = binascii.unhexlify(h.hexdigest())
Expand Down
2 changes: 1 addition & 1 deletion pynitrokey/libnk.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def friendly_name(self):

# string-conversion functions from/to C(++) @fixme: rename properly
c_enc = lambda x: x.encode("ascii") if isinstance(x, str) else x
py_enc = lambda x: ffi.string(x).decode() if not isinstance(x, str) else x
py_enc = lambda x: ffi.string(x).decode() if not isinstance(x, str) else x # type: ignore[union-attr]


class BaseLibNitrokey:
Expand Down
7 changes: 4 additions & 3 deletions pynitrokey/start/usb_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
"""

import sys
from typing import Mapping, Optional

import usb

field = ["Vendor", "Product", "Serial", "Revision", "Config", "Sys", "Board"]


def get_dict_for_device(dev: usb.Device) -> dict:
res = {}
def get_dict_for_device(dev: usb.Device) -> dict[str, Optional[str]]:
res: dict[str, Optional[str]] = {}
handle = dev.open()
res["name"] = dev.filename
for i, f in enumerate(field):
Expand All @@ -52,7 +53,7 @@ def get_devices() -> list:
return res


def print_device(dev: usb.Device, n: int = 8) -> None:
def print_device(dev: Mapping[str, Optional[str]], n: int = 8) -> None:
print("Device: %s" % dev["name"])
for i, f in enumerate(field):
if i > n:
Expand Down
19 changes: 1 addition & 18 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dev = [
"mypy >=1.4,<1.5",
"pyinstaller ~=6.5.0",
"pyinstaller-versionfile ==2.1.1; sys_platform=='win32'",
"types-cffi",
"types-requests",
"types-tqdm",
"pytest",
Expand Down Expand Up @@ -108,24 +109,6 @@ strict_equality = false
warn_unused_ignores = false
warn_return_any = false

# libraries without annotations
[[tool.mypy.overrides]]
module = [
"cbor.*",
"cffi.*",
"ecdsa.*",
"intelhex.*",
"nkdfu.*",
"ruamel.*",
"serial.*",
"usb.*",
"usb1.*",
"tlv8.*",
"pytest.*",
"smartcard.*",
]
ignore_missing_imports = true

[tool.pytest.ini_options]
log_cli = false
log_cli_level = "INFO"
Expand Down
23 changes: 23 additions & 0 deletions stubs/ecdsa.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Optional

class Curve:
order: Optional[int]

NIST256p = Curve()

class SigningKey:
@classmethod
def generate(cls, curve: Curve) -> "SigningKey": ...
@classmethod
def from_secret_exponent(cls, secexp: int, curve: Curve) -> "SigningKey": ...
@classmethod
def from_string(cls, string: bytes, curve: Curve) -> "SigningKey": ...
@classmethod
def from_pem(cls, string: str) -> "SigningKey": ...
def to_pem(self) -> bytes: ...
def get_verifying_key(self) -> "VerifyingKey": ...
def sign_digest(self, digest: bytes) -> bytes: ...

class VerifyingKey:
def to_string(self) -> bytes: ...
def to_pem(self) -> bytes: ...
30 changes: 30 additions & 0 deletions stubs/intelhex.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from array import array
from typing import BinaryIO, Literal, Optional, Sequence, TextIO, Tuple, Union, overload

from click import File

class IntelHex:
def __init__(self, source: Optional[str] = None) -> None: ...
@overload
def fromfile(self, fobj: Union[str, TextIO], format: Literal["hex"]) -> None: ...
@overload
def fromfile(self, fobj: Union[str, BinaryIO], format: Literal["bin"]) -> None: ...

loadfile = fromfile

@overload
def tofile(self, fobj: Union[str, TextIO], format: Literal["hex"]) -> None: ...
@overload
def tofile(self, fobj: Union[str, BinaryIO], format: Literal["bin"]) -> None: ...
def merge(self, other: "IntelHex", overlap: str = "error") -> None: ...
def tobinarray(
self, start: Optional[int] = None, size: Optional[int] = None
) -> array[int]: ...
def segments(self) -> Sequence[Tuple[int, int]]: ...
def minaddr(self) -> int: ...
def write_hex_file(self, f: Union[str, TextIO, File]) -> None: ...
@overload
def __getitem__(self, addr: int) -> int: ...
@overload
def __getitem__(self, addr: slice) -> "IntelHex": ...
def __setitem__(self, addr16: int, word: int) -> None: ...
Empty file added stubs/nkdfu/__init__.pyi
Empty file.
10 changes: 10 additions & 0 deletions stubs/nkdfu/dfu.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from array import array
from typing import Literal

from usb1 import USBDeviceHandle

class DFUBadSate(Exception): ...

class DFU:
def __init__(self, handle: USBDeviceHandle) -> None: ...
def download(self, data: array[int]) -> Literal["Finished"]: ...
13 changes: 13 additions & 0 deletions stubs/serial.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import Optional

class SerialException(IOError): ...

class Serial:
def __init__(
self,
port: Optional[str] = None,
baudrate: int = 9600,
timeout: Optional[float] = None,
) -> None: ...
def read(self, size: int) -> bytes: ...
def close(self) -> None: ...
2 changes: 2 additions & 0 deletions stubs/tlv8.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Entry:
def __init__(self, type_id: int, data: object) -> None: ...
9 changes: 9 additions & 0 deletions stubs/usb/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .core import USBError as USBError

class Device:
filename: str

def open(self) -> "DeviceHandle": ...

class DeviceHandle:
def getString(self, index: int, length: int) -> bytes: ...
1 change: 1 addition & 0 deletions stubs/usb/core.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class USBError(IOError): ...
16 changes: 16 additions & 0 deletions stubs/usb1.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import Any, Optional

class USBContext:
def __enter__(self) -> "USBContext": ...
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: ...
def getDeviceList(self) -> list["USBDevice"]: ...
def getByVendorIDAndProductID(
self, vendor_id: int, product_id: int
) -> Optional["USBDevice"]: ...

class USBDevice:
def getVendorID(self) -> int: ...
def getProductID(self) -> int: ...
def open(self) -> "USBDeviceHandle": ...

class USBDeviceHandle: ...

0 comments on commit a55974c

Please sign in to comment.