From 5b6fa1136ac77e4aa99e80a7757b443715395ae3 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Tue, 6 Aug 2019 15:42:23 +0200 Subject: [PATCH] core: improve typing annotations --- core/src/apps/management/change_pin.py | 14 +++++++++----- .../src/apps/management/recovery_device/recover.py | 6 ++++-- core/src/main.py | 4 ++-- core/src/protobuf.py | 11 ++++++++--- core/src/trezor/ui/__init__.py | 4 ++-- core/src/trezor/ui/loader.py | 6 ++++-- 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/core/src/apps/management/change_pin.py b/core/src/apps/management/change_pin.py index 59388657d..793eef62a 100644 --- a/core/src/apps/management/change_pin.py +++ b/core/src/apps/management/change_pin.py @@ -10,8 +10,12 @@ from trezor.ui.text import Text from apps.common.confirm import require_confirm from apps.common.request_pin import PinCancelled, request_pin +if False: + from typing import Any + from trezor.messages.ChangePin import ChangePin -async def change_pin(ctx, msg): + +async def change_pin(ctx: wire.Context, msg: ChangePin) -> Success: # confirm that user wants to change the pin await require_confirm_change_pin(ctx, msg) @@ -42,7 +46,7 @@ async def change_pin(ctx, msg): return Success(message="PIN removed") -def require_confirm_change_pin(ctx, msg): +def require_confirm_change_pin(ctx: wire.Context, msg: ChangePin) -> None: has_pin = config.has_pin() if msg.remove and has_pin: # removing pin @@ -64,7 +68,7 @@ def require_confirm_change_pin(ctx, msg): return require_confirm(ctx, text) -async def request_pin_confirm(ctx: wire.Context, *args, **kwargs) -> str: +async def request_pin_confirm(ctx: wire.Context, *args: Any, **kwargs: Any) -> str: while True: pin1 = await request_pin_ack(ctx, "Enter new PIN", *args, **kwargs) pin2 = await request_pin_ack(ctx, "Re-enter new PIN", *args, **kwargs) @@ -73,7 +77,7 @@ async def request_pin_confirm(ctx: wire.Context, *args, **kwargs) -> str: await pin_mismatch() -async def request_pin_ack(ctx: wire.Context, *args, **kwargs) -> str: +async def request_pin_ack(ctx: wire.Context, *args: Any, **kwargs: Any) -> str: try: await ctx.call(ButtonRequest(code=ButtonRequestType.Other), ButtonAck) return await ctx.wait(request_pin(*args, **kwargs)) @@ -81,7 +85,7 @@ async def request_pin_ack(ctx: wire.Context, *args, **kwargs) -> str: raise wire.ActionCancelled("Cancelled") -async def pin_mismatch(): +async def pin_mismatch() -> None: text = Text("PIN mismatch", ui.ICON_WRONG, ui.RED) text.normal("The PINs you entered", "do not match.") text.normal("") diff --git a/core/src/apps/management/recovery_device/recover.py b/core/src/apps/management/recovery_device/recover.py index f772f699c..b6356014c 100644 --- a/core/src/apps/management/recovery_device/recover.py +++ b/core/src/apps/management/recovery_device/recover.py @@ -39,8 +39,10 @@ def _process_slip39(words: str) -> Optional[bytes]: if threshold == 1: raise ValueError("Threshold equal to 1 is not allowed.") + remaining = storage.recovery.get_remaining() + # if this is the first share, parse and store metadata - if not storage.recovery.get_remaining(): + if not remaining: storage.recovery.set_slip39_iteration_exponent(iteration_exponent) storage.recovery.set_slip39_identifier(identifier) storage.recovery.set_slip39_threshold(threshold) @@ -55,7 +57,7 @@ def _process_slip39(words: str) -> Optional[bytes]: raise RuntimeError("Slip39: This mnemonic was already entered") # add mnemonic to storage - remaining = storage.recovery.get_remaining() - 1 + remaining -= 1 storage.recovery.set_remaining(remaining) storage.recovery_shares.set(index, words) if remaining != 0: diff --git a/core/src/main.py b/core/src/main.py index 7f1b30b90..737661825 100644 --- a/core/src/main.py +++ b/core/src/main.py @@ -15,7 +15,7 @@ usb.bus.open() utils.set_mode_unprivileged() -def _boot_recovery(): +def _boot_recovery() -> None: # load applications import apps.homescreen @@ -27,7 +27,7 @@ def _boot_recovery(): loop.schedule(recovery_homescreen()) -def _boot_default(): +def _boot_default() -> None: # load applications import apps.homescreen import apps.management diff --git a/core/src/protobuf.py b/core/src/protobuf.py index 0bb1a3790..3d7718c37 100644 --- a/core/src/protobuf.py +++ b/core/src/protobuf.py @@ -6,7 +6,7 @@ bytes, string, embedded message and repeated fields. from micropython import const if False: - from typing import Any, Dict, List, Type, TypeVar + from typing import Any, Dict, Iterable, List, Type, TypeVar, Union from typing_extensions import Protocol class AsyncReader(Protocol): @@ -124,7 +124,7 @@ class BoolType: class EnumType: WIRE_TYPE = 0 - def __init__(self, _, enum_values): + def __init__(self, name: str, enum_values: Iterable[int]) -> None: self.enum_values = enum_values def validate(self, fvalue: int) -> int: @@ -190,6 +190,11 @@ async def load_message( fields = msg_type.get_fields() msg = msg_type() + if False: + SingularValue = Union[int, bool, bytearray, str, MessageType] + Value = Union[SingularValue, List[SingularValue]] + fvalue = 0 # type: Value + while True: try: fkey = await load_uvarint(reader) @@ -276,7 +281,7 @@ async def dump_message( if isinstance(ftype, type) and issubclass(ftype, MessageType): ffields = ftype.get_fields() else: - ffields = None + del ffields for svalue in fvalue: await dump_uvarint(writer, fkey) diff --git a/core/src/trezor/ui/__init__.py b/core/src/trezor/ui/__init__.py index b03250fbf..d89c8ad73 100644 --- a/core/src/trezor/ui/__init__.py +++ b/core/src/trezor/ui/__init__.py @@ -108,7 +108,7 @@ def header( display.text(44, 35, title, BOLD, fg, bg) -def header_warning(message: str, clear=True) -> None: +def header_warning(message: str, clear: bool = True) -> None: # TODO: review: is the clear=True really needed? display.bar(0, 0, WIDTH, 30, style.YELLOW) display.text_center(WIDTH // 2, 22, message, BOLD, style.BLACK, style.YELLOW) @@ -116,7 +116,7 @@ def header_warning(message: str, clear=True) -> None: display.bar(0, 30, WIDTH, HEIGHT - 30, style.BG) -def header_error(message: str, clear=True) -> None: +def header_error(message: str, clear: bool = True) -> None: # TODO: review: as above display.bar(0, 0, WIDTH, 30, style.RED) display.text_center(WIDTH // 2, 22, message, BOLD, style.WHITE, style.RED) diff --git a/core/src/trezor/ui/loader.py b/core/src/trezor/ui/loader.py index bfc8c3442..bb21955d7 100644 --- a/core/src/trezor/ui/loader.py +++ b/core/src/trezor/ui/loader.py @@ -42,8 +42,8 @@ class Loader(ui.Control): self.normal_style = style.normal self.active_style = style.active self.target_ms = _TARGET_MS - self.start_ms = None - self.stop_ms = None + self.start_ms = None # type: Optional[int] + self.stop_ms = None # type: Optional[int] def start(self) -> None: self.start_ms = utime.ticks_ms() @@ -62,6 +62,8 @@ class Loader(ui.Control): target = self.target_ms start = self.start_ms stop = self.stop_ms + if start is None: + return now = utime.ticks_ms() if stop is None: r = min(now - start, target)