1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-27 01:48:17 +00:00

core: improve typing annotations

This commit is contained in:
Jan Pochyla 2019-08-06 15:42:23 +02:00
parent 0a594ea1e9
commit 5b6fa1136a
6 changed files with 29 additions and 16 deletions

View File

@ -10,8 +10,12 @@ from trezor.ui.text import Text
from apps.common.confirm import require_confirm from apps.common.confirm import require_confirm
from apps.common.request_pin import PinCancelled, request_pin 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 # confirm that user wants to change the pin
await require_confirm_change_pin(ctx, msg) await require_confirm_change_pin(ctx, msg)
@ -42,7 +46,7 @@ async def change_pin(ctx, msg):
return Success(message="PIN removed") 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() has_pin = config.has_pin()
if msg.remove and has_pin: # removing 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) 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: while True:
pin1 = await request_pin_ack(ctx, "Enter new PIN", *args, **kwargs) pin1 = await request_pin_ack(ctx, "Enter new PIN", *args, **kwargs)
pin2 = await request_pin_ack(ctx, "Re-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() 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: try:
await ctx.call(ButtonRequest(code=ButtonRequestType.Other), ButtonAck) await ctx.call(ButtonRequest(code=ButtonRequestType.Other), ButtonAck)
return await ctx.wait(request_pin(*args, **kwargs)) 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") raise wire.ActionCancelled("Cancelled")
async def pin_mismatch(): async def pin_mismatch() -> None:
text = Text("PIN mismatch", ui.ICON_WRONG, ui.RED) text = Text("PIN mismatch", ui.ICON_WRONG, ui.RED)
text.normal("The PINs you entered", "do not match.") text.normal("The PINs you entered", "do not match.")
text.normal("") text.normal("")

View File

@ -39,8 +39,10 @@ def _process_slip39(words: str) -> Optional[bytes]:
if threshold == 1: if threshold == 1:
raise ValueError("Threshold equal to 1 is not allowed.") 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 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_iteration_exponent(iteration_exponent)
storage.recovery.set_slip39_identifier(identifier) storage.recovery.set_slip39_identifier(identifier)
storage.recovery.set_slip39_threshold(threshold) 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") raise RuntimeError("Slip39: This mnemonic was already entered")
# add mnemonic to storage # add mnemonic to storage
remaining = storage.recovery.get_remaining() - 1 remaining -= 1
storage.recovery.set_remaining(remaining) storage.recovery.set_remaining(remaining)
storage.recovery_shares.set(index, words) storage.recovery_shares.set(index, words)
if remaining != 0: if remaining != 0:

View File

@ -15,7 +15,7 @@ usb.bus.open()
utils.set_mode_unprivileged() utils.set_mode_unprivileged()
def _boot_recovery(): def _boot_recovery() -> None:
# load applications # load applications
import apps.homescreen import apps.homescreen
@ -27,7 +27,7 @@ def _boot_recovery():
loop.schedule(recovery_homescreen()) loop.schedule(recovery_homescreen())
def _boot_default(): def _boot_default() -> None:
# load applications # load applications
import apps.homescreen import apps.homescreen
import apps.management import apps.management

View File

@ -6,7 +6,7 @@ bytes, string, embedded message and repeated fields.
from micropython import const from micropython import const
if False: 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 from typing_extensions import Protocol
class AsyncReader(Protocol): class AsyncReader(Protocol):
@ -124,7 +124,7 @@ class BoolType:
class EnumType: class EnumType:
WIRE_TYPE = 0 WIRE_TYPE = 0
def __init__(self, _, enum_values): def __init__(self, name: str, enum_values: Iterable[int]) -> None:
self.enum_values = enum_values self.enum_values = enum_values
def validate(self, fvalue: int) -> int: def validate(self, fvalue: int) -> int:
@ -190,6 +190,11 @@ async def load_message(
fields = msg_type.get_fields() fields = msg_type.get_fields()
msg = msg_type() msg = msg_type()
if False:
SingularValue = Union[int, bool, bytearray, str, MessageType]
Value = Union[SingularValue, List[SingularValue]]
fvalue = 0 # type: Value
while True: while True:
try: try:
fkey = await load_uvarint(reader) fkey = await load_uvarint(reader)
@ -276,7 +281,7 @@ async def dump_message(
if isinstance(ftype, type) and issubclass(ftype, MessageType): if isinstance(ftype, type) and issubclass(ftype, MessageType):
ffields = ftype.get_fields() ffields = ftype.get_fields()
else: else:
ffields = None del ffields
for svalue in fvalue: for svalue in fvalue:
await dump_uvarint(writer, fkey) await dump_uvarint(writer, fkey)

View File

@ -108,7 +108,7 @@ def header(
display.text(44, 35, title, BOLD, fg, bg) 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? # TODO: review: is the clear=True really needed?
display.bar(0, 0, WIDTH, 30, style.YELLOW) display.bar(0, 0, WIDTH, 30, style.YELLOW)
display.text_center(WIDTH // 2, 22, message, BOLD, style.BLACK, 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) 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 # TODO: review: as above
display.bar(0, 0, WIDTH, 30, style.RED) display.bar(0, 0, WIDTH, 30, style.RED)
display.text_center(WIDTH // 2, 22, message, BOLD, style.WHITE, style.RED) display.text_center(WIDTH // 2, 22, message, BOLD, style.WHITE, style.RED)

View File

@ -42,8 +42,8 @@ class Loader(ui.Control):
self.normal_style = style.normal self.normal_style = style.normal
self.active_style = style.active self.active_style = style.active
self.target_ms = _TARGET_MS self.target_ms = _TARGET_MS
self.start_ms = None self.start_ms = None # type: Optional[int]
self.stop_ms = None self.stop_ms = None # type: Optional[int]
def start(self) -> None: def start(self) -> None:
self.start_ms = utime.ticks_ms() self.start_ms = utime.ticks_ms()
@ -62,6 +62,8 @@ class Loader(ui.Control):
target = self.target_ms target = self.target_ms
start = self.start_ms start = self.start_ms
stop = self.stop_ms stop = self.stop_ms
if start is None:
return
now = utime.ticks_ms() now = utime.ticks_ms()
if stop is None: if stop is None:
r = min(now - start, target) r = min(now - start, target)