mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-19 04:48:12 +00:00
chore(python): better static typing for debuglink and emulator
[no changelog]
This commit is contained in:
parent
bd6f3d5f46
commit
dc22f98aed
@ -72,7 +72,8 @@ class Emulator:
|
|||||||
else:
|
else:
|
||||||
self.logfile = self.profile_dir / "trezor.log"
|
self.logfile = self.profile_dir / "trezor.log"
|
||||||
|
|
||||||
self.client: Optional[TrezorClientDebugLink] = None
|
# Using `client` property instead to assert `not None`
|
||||||
|
self._client: Optional[TrezorClientDebugLink] = None
|
||||||
self.process: Optional[subprocess.Popen] = None
|
self.process: Optional[subprocess.Popen] = None
|
||||||
|
|
||||||
self.port = 21324
|
self.port = 21324
|
||||||
@ -81,6 +82,16 @@ class Emulator:
|
|||||||
self.auto_interact = auto_interact
|
self.auto_interact = auto_interact
|
||||||
self.extra_args = list(extra_args)
|
self.extra_args = list(extra_args)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def client(self) -> TrezorClientDebugLink:
|
||||||
|
"""So that type-checkers do not see `client` as `Optional`.
|
||||||
|
|
||||||
|
(it is not None between `start()` and `stop()` calls)
|
||||||
|
"""
|
||||||
|
if self._client is None:
|
||||||
|
raise RuntimeError
|
||||||
|
return self._client
|
||||||
|
|
||||||
def make_args(self) -> List[str]:
|
def make_args(self) -> List[str]:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@ -162,14 +173,15 @@ class Emulator:
|
|||||||
(self.profile_dir / "trezor.port").write_text(str(self.port) + "\n")
|
(self.profile_dir / "trezor.port").write_text(str(self.port) + "\n")
|
||||||
|
|
||||||
transport = self._get_transport()
|
transport = self._get_transport()
|
||||||
self.client = TrezorClientDebugLink(transport, auto_interact=self.auto_interact)
|
self._client = TrezorClientDebugLink(
|
||||||
|
transport, auto_interact=self.auto_interact
|
||||||
self.client.open()
|
)
|
||||||
|
self._client.open()
|
||||||
|
|
||||||
def stop(self) -> None:
|
def stop(self) -> None:
|
||||||
if self.client:
|
if self._client:
|
||||||
self.client.close()
|
self._client.close()
|
||||||
self.client = None
|
self._client = None
|
||||||
|
|
||||||
if self.process:
|
if self.process:
|
||||||
LOG.info("Terminating emulator...")
|
LOG.info("Terminating emulator...")
|
||||||
|
@ -36,9 +36,11 @@ from typing import (
|
|||||||
Tuple,
|
Tuple,
|
||||||
Type,
|
Type,
|
||||||
Union,
|
Union,
|
||||||
|
overload,
|
||||||
)
|
)
|
||||||
|
|
||||||
from mnemonic import Mnemonic
|
from mnemonic import Mnemonic
|
||||||
|
from typing_extensions import Literal
|
||||||
|
|
||||||
from . import mapping, messages, protobuf
|
from . import mapping, messages, protobuf
|
||||||
from .client import TrezorClient
|
from .client import TrezorClient
|
||||||
@ -296,6 +298,17 @@ class DebugLink:
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
# Type overloads make sure that when we supply `wait=True` into `click()`,
|
||||||
|
# it will always return `LayoutContent` and we do not need to assert `is not None`.
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def click(self, click: Tuple[int, int]) -> None:
|
||||||
|
...
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def click(self, click: Tuple[int, int], wait: Literal[True]) -> LayoutContent:
|
||||||
|
...
|
||||||
|
|
||||||
def click(
|
def click(
|
||||||
self, click: Tuple[int, int], wait: bool = False
|
self, click: Tuple[int, int], wait: bool = False
|
||||||
) -> Optional[LayoutContent]:
|
) -> Optional[LayoutContent]:
|
||||||
|
Loading…
Reference in New Issue
Block a user