refactor(core): add type annotation to `spawn()` and `with_context()`
What changed, and why it matters
This commit only adds Python type annotations to two internal functions. It does not change any runtime behavior, logic, or security checks in the Trezor firmware.
No action required. This is a non-security refactoring change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds generic type variables to spawn() in core/src/trezor/workflow.py and with_context() in core/src/trezor/wire/context.py. These are static typing changes (loop.Task[T], loop.spawn[T], Generator[Any, Any, T]) and have no effect on executed code. No functional code paths were modified.
Changed components
core/src/trezor/workflow.pycore/src/trezor/wire/context.pyInspect captured patch +4 / −3
diff --git a/core/src/trezor/wire/context.py b/core/src/trezor/wire/context.py
index f0469e2a..cab7b4eb 100644
--- a/core/src/trezor/wire/context.py
+++ b/core/src/trezor/wire/context.py
@@ -112,7 +112,7 @@ if utils.USE_THP:
return ctx.channel
-def with_context(ctx: Context, workflow: loop.Task) -> Generator:
+def with_context(ctx: Context, workflow: loop.Task[T]) -> Generator[Any, Any, T]:
"""Run a workflow in a particular context.
Stores the context in a closure and installs it into the global variable every time
diff --git a/core/src/trezor/workflow.py b/core/src/trezor/workflow.py
index 41b7d94b..7540af85 100644
--- a/core/src/trezor/workflow.py
+++ b/core/src/trezor/workflow.py
@@ -6,9 +6,10 @@ from trezor import log, loop, utils
from trezor.enums import MessageType
if TYPE_CHECKING:
- from typing import Callable
+ from typing import Callable, TypeVar
IdleCallback = Callable[[], None]
+ T = TypeVar("T")
if __debug__:
# Used in `on_close` below for memory statistics.
@@ -81,7 +82,7 @@ def _on_close(workflow: loop.spawn) -> None:
micropython.mem_info()
-def spawn(workflow: loop.Task) -> loop.spawn:
+def spawn(workflow: loop.Task[T]) -> loop.spawn[T]:
"""Spawn a workflow task.
Creates an instance of loop.spawn for the workflow and registers it into the
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.