From c25a41aa576f6140d827676980d70bc06aa85083 Mon Sep 17 00:00:00 2001 From: Andrew Kozlik Date: Wed, 9 Oct 2019 17:07:25 +0200 Subject: [PATCH] core: Fix mypy warnings in FatFS and SD salt code. --- core/embed/extmod/modtrezorio/modtrezorio-fatfs.h | 14 ++++++++++++-- core/mocks/generated/trezorio.pyi | 14 ++++++++++++-- core/src/apps/common/sd_salt.py | 10 ++++++---- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/core/embed/extmod/modtrezorio/modtrezorio-fatfs.h b/core/embed/extmod/modtrezorio/modtrezorio-fatfs.h index a8d64b51ef..570c77c6ca 100644 --- a/core/embed/extmod/modtrezorio/modtrezorio-fatfs.h +++ b/core/embed/extmod/modtrezorio/modtrezorio-fatfs.h @@ -125,7 +125,17 @@ typedef struct _mp_obj_FatFSFile_t { FIL fp; } mp_obj_FatFSFile_t; -/// def __exit__(self) -> None: +/// def __enter__(self) -> FatFSFile: +/// """ +/// Return an open file object +/// """ + +/// from types import TracebackType +/// def __exit__( +/// self, type: Optional[Type[BaseException]], +/// value: Optional[BaseException], +/// traceback: Optional[TracebackType], +/// ) -> None: /// """ /// Close an open file object /// """ @@ -175,7 +185,7 @@ STATIC mp_obj_t mod_trezorio_FatFSFile_read(mp_obj_t self, mp_obj_t data) { STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_FatFSFile_read_obj, mod_trezorio_FatFSFile_read); -/// def write(self, data: bytearray) -> int: +/// def write(self, data: Union[bytes, bytearray]) -> int: /// """ /// Write data to the file /// """ diff --git a/core/mocks/generated/trezorio.pyi b/core/mocks/generated/trezorio.pyi index 81833cfa07..eb6d2d69ee 100644 --- a/core/mocks/generated/trezorio.pyi +++ b/core/mocks/generated/trezorio.pyi @@ -7,7 +7,17 @@ class FatFSFile: Class encapsulating file """ - def __exit__(self) -> None: + def __enter__(self) -> FatFSFile: + """ + Return an open file object + """ + from types import TracebackType + + def __exit__( + self, type: Optional[Type[BaseException]], + value: Optional[BaseException], + traceback: Optional[TracebackType], + ) -> None: """ Close an open file object """ @@ -22,7 +32,7 @@ class FatFSFile: Read data from the file """ - def write(self, data: bytearray) -> int: + def write(self, data: Union[bytes, bytearray]) -> int: """ Write data to the file """ diff --git a/core/src/apps/common/sd_salt.py b/core/src/apps/common/sd_salt.py index f9aedba287..09b26eb47a 100644 --- a/core/src/apps/common/sd_salt.py +++ b/core/src/apps/common/sd_salt.py @@ -45,11 +45,11 @@ async def _insert_card_dialog(ctx: Optional[wire.Context]) -> None: await require_confirm(ctx, text, confirm=None, cancel="Close") -def _get_device_dir() -> bytes: +def _get_device_dir() -> str: return "/trezor/device_%s" % storage.device.get_device_id().lower() -def _get_salt_path(new: bool = False) -> bytes: +def _get_salt_path(new: bool = False) -> str: if new: return "%s/salt.new" % _get_device_dir() else: @@ -72,9 +72,10 @@ async def request_sd_salt( fs.mount() # Load salt if it exists. + salt = None # type: Optional[bytearray] try: with fs.open(salt_path, "r") as f: - salt = bytearray(SD_SALT_LEN_BYTES) # type: Optional[bytearray] + salt = bytearray(SD_SALT_LEN_BYTES) salt_tag = bytearray(SD_SALT_AUTH_TAG_LEN_BYTES) f.read(salt) f.read(salt_tag) @@ -88,9 +89,10 @@ async def request_sd_salt( return salt # Load salt.new if it exists. + new_salt = None # type: Optional[bytearray] try: with fs.open(new_salt_path, "r") as f: - new_salt = bytearray(SD_SALT_LEN_BYTES) # type: Optional[bytearray] + new_salt = bytearray(SD_SALT_LEN_BYTES) new_salt_tag = bytearray(SD_SALT_AUTH_TAG_LEN_BYTES) f.read(new_salt) f.read(new_salt_tag)