From 31052007ffd95a5f50d42b6622dc79d0237308c6 Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Wed, 24 Mar 2021 18:11:58 +0100 Subject: [PATCH] refactor(core): enable mypy for apps.management --- core/Makefile | 1 + core/src/apps/management/apply_flags.py | 5 +++- core/src/apps/management/apply_settings.py | 30 ++++++++++++++-------- core/src/apps/management/backup_device.py | 7 ++++- core/src/apps/management/change_pin.py | 4 ++- core/src/apps/management/wipe_device.py | 6 ++++- core/src/trezor/ui/layouts/tt/__init__.py | 2 +- 7 files changed, 40 insertions(+), 15 deletions(-) diff --git a/core/Makefile b/core/Makefile index f7a4939e5c..26b04d51b2 100644 --- a/core/Makefile +++ b/core/Makefile @@ -109,6 +109,7 @@ mypy: src/main.py \ src/apps/bitcoin \ src/apps/cardano \ + src/apps/management \ src/apps/misc \ src/apps/webauthn \ src/trezor/ui diff --git a/core/src/apps/management/apply_flags.py b/core/src/apps/management/apply_flags.py index 66aa059206..ab502d4a9a 100644 --- a/core/src/apps/management/apply_flags.py +++ b/core/src/apps/management/apply_flags.py @@ -3,8 +3,11 @@ from storage.device import set_flags from trezor import wire from trezor.messages import Success +if False: + from trezor.messages import ApplyFlags -async def apply_flags(ctx, msg): + +async def apply_flags(ctx: wire.GenericContext, msg: ApplyFlags) -> Success: if not storage.device.is_initialized(): raise wire.NotInitialized("Device is not initialized") set_flags(msg.flags) diff --git a/core/src/apps/management/apply_settings.py b/core/src/apps/management/apply_settings.py index af3dd6ee53..022d5489bf 100644 --- a/core/src/apps/management/apply_settings.py +++ b/core/src/apps/management/apply_settings.py @@ -33,7 +33,7 @@ def validate_homescreen(homescreen: bytes) -> None: raise wire.DataError("Homescreen must be full-color TOIF image") -async def apply_settings(ctx: wire.Context, msg: ApplySettings): +async def apply_settings(ctx: wire.Context, msg: ApplySettings) -> Success: if not storage.device.is_initialized(): raise wire.NotInitialized("Device is not initialized") if ( @@ -99,7 +99,7 @@ async def apply_settings(ctx: wire.Context, msg: ApplySettings): return Success(message="Settings applied") -async def require_confirm_change_homescreen(ctx): +async def require_confirm_change_homescreen(ctx: wire.GenericContext) -> None: await confirm_action( ctx, "set_homescreen", @@ -109,7 +109,7 @@ async def require_confirm_change_homescreen(ctx): ) -async def require_confirm_change_label(ctx, label): +async def require_confirm_change_label(ctx: wire.GenericContext, label: str) -> None: await confirm_action( ctx, "set_label", @@ -120,7 +120,9 @@ async def require_confirm_change_label(ctx, label): ) -async def require_confirm_change_passphrase(ctx, use): +async def require_confirm_change_passphrase( + ctx: wire.GenericContext, use: bool +) -> None: if use: description = "Do you really want to enable passphrase encryption?" else: @@ -135,8 +137,8 @@ async def require_confirm_change_passphrase(ctx, use): async def require_confirm_change_passphrase_source( - ctx, passphrase_always_on_device: bool -): + ctx: wire.GenericContext, passphrase_always_on_device: bool +) -> None: if passphrase_always_on_device: description = "Do you really want to enter passphrase always on the device?" else: @@ -150,7 +152,9 @@ async def require_confirm_change_passphrase_source( ) -async def require_confirm_change_display_rotation(ctx, rotation): +async def require_confirm_change_display_rotation( + ctx: wire.GenericContext, rotation: int +) -> None: if rotation == 0: label = "north" elif rotation == 90: @@ -171,7 +175,9 @@ async def require_confirm_change_display_rotation(ctx, rotation): ) -async def require_confirm_change_autolock_delay(ctx, delay_ms): +async def require_confirm_change_autolock_delay( + ctx: wire.GenericContext, delay_ms: int +) -> None: await confirm_action( ctx, "set_autolock_delay", @@ -182,7 +188,9 @@ async def require_confirm_change_autolock_delay(ctx, delay_ms): ) -async def require_confirm_safety_checks(ctx, level: SafetyCheckLevel) -> None: +async def require_confirm_safety_checks( + ctx: wire.GenericContext, level: SafetyCheckLevel +) -> None: if level == SafetyCheckLevel.PromptAlways: await confirm_action( ctx, @@ -220,7 +228,9 @@ async def require_confirm_safety_checks(ctx, level: SafetyCheckLevel) -> None: raise ValueError # enum value out of range -async def require_confirm_experimental_features(ctx, enable: bool) -> None: +async def require_confirm_experimental_features( + ctx: wire.GenericContext, enable: bool +) -> None: if enable: await confirm_action( ctx, diff --git a/core/src/apps/management/backup_device.py b/core/src/apps/management/backup_device.py index 20a73f2a3e..b278c8843b 100644 --- a/core/src/apps/management/backup_device.py +++ b/core/src/apps/management/backup_device.py @@ -7,14 +7,19 @@ from apps.common import mnemonic from .reset_device import backup_seed, layout +if False: + from trezor.messages import BackupDevice -async def backup_device(ctx, msg): + +async def backup_device(ctx: wire.Context, msg: BackupDevice) -> Success: if not storage.device.is_initialized(): raise wire.NotInitialized("Device is not initialized") if not storage.device.needs_backup(): raise wire.ProcessError("Seed already backed up") mnemonic_secret, mnemonic_type = mnemonic.get() + if mnemonic_secret is None: + raise RuntimeError storage.device.set_unfinished_backup(True) storage.device.set_backed_up() diff --git a/core/src/apps/management/change_pin.py b/core/src/apps/management/change_pin.py index 0230c70861..1eb9df64e0 100644 --- a/core/src/apps/management/change_pin.py +++ b/core/src/apps/management/change_pin.py @@ -11,6 +11,8 @@ from apps.common.request_pin import ( ) if False: + from typing import Awaitable + from trezor.messages import ChangePin @@ -57,7 +59,7 @@ async def change_pin(ctx: wire.Context, msg: ChangePin) -> Success: return Success(message=msg_wire) -def require_confirm_change_pin(ctx: wire.Context, msg: ChangePin) -> None: +def require_confirm_change_pin(ctx: wire.Context, msg: ChangePin) -> Awaitable[None]: has_pin = config.has_pin() if msg.remove and has_pin: # removing pin diff --git a/core/src/apps/management/wipe_device.py b/core/src/apps/management/wipe_device.py index 425e905283..5778209613 100644 --- a/core/src/apps/management/wipe_device.py +++ b/core/src/apps/management/wipe_device.py @@ -6,8 +6,12 @@ from trezor.ui.layouts import confirm_action from .apply_settings import reload_settings_from_storage +if False: + from trezor import wire + from trezor.messages import WipeDevice -async def wipe_device(ctx, msg): + +async def wipe_device(ctx: wire.GenericContext, msg: WipeDevice) -> Success: await confirm_action( ctx, "confirm_wipe", diff --git a/core/src/trezor/ui/layouts/tt/__init__.py b/core/src/trezor/ui/layouts/tt/__init__.py index 05db8fa463..3bfec71d2a 100644 --- a/core/src/trezor/ui/layouts/tt/__init__.py +++ b/core/src/trezor/ui/layouts/tt/__init__.py @@ -1093,7 +1093,7 @@ async def request_passphrase_on_device(ctx: wire.GenericContext, max_len: int) - async def request_pin_on_device( ctx: wire.GenericContext, prompt: str, - attempts_remaining: int, + attempts_remaining: Optional[int], allow_cancel: bool, ) -> str: await button_request(ctx, "pin_device", code=ButtonRequestType.PinEntry)