Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type hints for Python 3.11 and older #611

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions pynitrokey/start/gnuk_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@
from array import array
from contextlib import contextmanager
from struct import *
from typing import Iterator, Optional, Tuple
from typing import TYPE_CHECKING, Iterator, Optional, Tuple

import usb

# Possible Gnuk Token products
from pynitrokey.start.usb_strings import get_dict_for_device

# Workaround for compatibility with Python 3.11 and older, see:
# https://github.com/python/mypy/issues/13942
if TYPE_CHECKING:
IntArray = array[int]
else:
IntArray = array

USB_PRODUCT_LIST = [
{"vendor": 0x234B, "product": 0x0000}, # FSIJ Gnuk Token
{"vendor": 0x20A0, "product": 0x4211}, # Nitrokey Start
Expand Down Expand Up @@ -230,7 +237,7 @@ def execute(self, last_addr):
requestType=0x40, request=2, buffer=None, value=i, index=o, timeout=10
)

def icc_get_result(self) -> Tuple[int, int, array[int]]:
def icc_get_result(self) -> Tuple[int, int, IntArray]:
usbmsg = self.__devhandle.bulkRead(self.__bulkin, 1024, self.__timeout)
if len(usbmsg) < 10:
self.local_print(usbmsg, True) # type: ignore[arg-type]
Expand All @@ -255,7 +262,7 @@ def icc_get_status(self) -> int:
# XXX: check chain, data
return status

def icc_power_on(self) -> array[int]:
def icc_power_on(self) -> IntArray:
msg = icc_compose(0x62, 0, 0, self.__seq, 0, b"")
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
self.increment_seq()
Expand All @@ -272,13 +279,13 @@ def icc_power_off(self) -> int:
# XXX: check chain, data
return status

def icc_send_data_block(self, data) -> Tuple[int, int, array[int]]:
def icc_send_data_block(self, data) -> Tuple[int, int, IntArray]:
msg = icc_compose(0x6F, len(data), 0, self.__seq, 0, data)
self.__devhandle.bulkWrite(self.__bulkout, msg, self.__timeout)
self.increment_seq()
return self.icc_get_result()

def icc_send_cmd(self, data) -> array[int]:
def icc_send_cmd(self, data) -> IntArray:
status, chain, data_rcv = self.icc_send_data_block(data)
if chain == 0:
while status == 0x80:
Expand Down
Loading