style(python): improve type annotations at trezorlib.transport
What changed, and why it matters
This is a minor code cleanup that improves Python type annotations in the Trezor library's transport layer. It does not change how the software behaves or fix any security issue.
No security action required; treat as ordinary style/maintenance commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors type hints in trezorlib.transport: replaces TypeVar-based classmethod signatures with typing.Self, moves ClassVar declarations, removes an unused import, and normalizes an annotated class variable assignment. No runtime logic, parsing, serialization, cryptography, or access control is altered.
Changed components
python/src/trezorlib/transport/__init__.pypython/src/trezorlib/transport/ble.pypython/src/trezorlib/transport/bridge.pyInspect captured patch +11 / −10
diff --git a/python/src/trezorlib/transport/__init__.py b/python/src/trezorlib/transport/__init__.py
index db2f2441..273464b6 100644
--- a/python/src/trezorlib/transport/__init__.py
+++ b/python/src/trezorlib/transport/__init__.py
@@ -17,7 +17,6 @@
from __future__ import annotations
import logging
-import os
import typing as t
from ..exceptions import TrezorException
@@ -52,16 +51,18 @@ class Timeout(TransportException):
class Transport:
- PATH_PREFIX: str
+ PATH_PREFIX: t.ClassVar[str]
+ CHUNK_SIZE: t.ClassVar[int | None]
+ ENABLED: t.ClassVar[bool]
@classmethod
def enumerate(
- cls: t.Type[T], models: t.Iterable[TrezorModel] | None = None
- ) -> t.Iterable[T]:
+ cls, models: t.Iterable[TrezorModel] | None = None
+ ) -> t.Iterable[t.Self]:
raise NotImplementedError
@classmethod
- def find_by_path(cls: t.Type[T], path: str, prefix_search: bool = False) -> T:
+ def find_by_path(cls, path: str, prefix_search: bool = False) -> t.Self:
for device in cls.enumerate():
if device.get_path() == path:
@@ -75,7 +76,8 @@ class Transport:
def get_path(self) -> str:
raise NotImplementedError
- def find_debug(self: T) -> T:
+ # find_debug is allowed to return a different type than Self
+ def find_debug(self) -> Transport:
raise NotImplementedError
def open(self) -> None:
@@ -93,8 +95,6 @@ class Transport:
def ping(self) -> bool:
raise NotImplementedError
- CHUNK_SIZE: t.ClassVar[int | None]
-
def all_transports() -> t.Iterable[type[Transport]]:
from .ble import BleTransport
@@ -116,7 +116,7 @@ def all_transports() -> t.Iterable[type[Transport]]:
def enumerate_devices(
models: t.Iterable[TrezorModel] | None = None,
) -> t.Sequence[Transport]:
- devices: t.List[Transport] = []
+ devices: list[Transport] = []
for transport in all_transports():
name = transport.__name__
try:
diff --git a/python/src/trezorlib/transport/ble.py b/python/src/trezorlib/transport/ble.py
index 12c01d6f..d2883207 100644
--- a/python/src/trezorlib/transport/ble.py
+++ b/python/src/trezorlib/transport/ble.py
@@ -53,6 +53,7 @@ SHUTDOWN_TIMEOUT_SECONDS = 10
class BleTransport(Transport):
+
ENABLED = BLEAK_IMPORTED
PATH_PREFIX = "ble"
CHUNK_SIZE = 244
diff --git a/python/src/trezorlib/transport/bridge.py b/python/src/trezorlib/transport/bridge.py
index 211de311..38b71735 100644
--- a/python/src/trezorlib/transport/bridge.py
+++ b/python/src/trezorlib/transport/bridge.py
@@ -122,7 +122,7 @@ class BridgeTransport(Transport):
"""
PATH_PREFIX = "bridge"
- ENABLED: bool = True
+ ENABLED = True
CHUNK_SIZE = None
def __init__(
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.