chore(core): remove last uses of `Optional`
What changed, and why it matters
This is a routine code cleanup that replaces the older Optional[Type] style with the newer Type | None style in a few Python files. It also updates the project's automated style checker to enforce the newer style. There is no functional change to the code and no security impact.
No security action needed. This is a style-only refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit removes remaining uses of typing.Optional in favor of PEP 604 union syntax (X | None). It updates core/src/apps/bitcoin/sign_tx/approvers.py and core/tools/analyze-memory-dump.py accordingly, and adjusts pyproject.toml to enable Ruff rules UP007 and UP045 for core/*/.py while keeping them disabled for other paths that still support Python 3.9. Makefile labels are updated from ‘RUFF - PEP 585’ to ‘RUFF’. No runtime behavior changes.
Changed components
core/src/apps/bitcoin/sign_tx/approvers.pycore/tools/analyze-memory-dump.pypyproject.tomlMakefileInspect captured patch +16 / −8
### Makefile
@@ -68,7 +68,7 @@ pystyle_check: ## run code style check on application sources and tests
@isort --check-only $(PY_FILES)
@echo [BLACK]
@black --check $(BLACK_FLAGS) $(PY_FILES)
- @echo [RUFF - PEP 585]
+ @echo [RUFF]
@ruff check $(PY_FILES)
@echo [PYLINT]
@pylint $(PY_FILES)
@@ -85,7 +85,7 @@ pystyle: ## apply code style on application sources and tests
@isort $(PY_FILES)
@echo [BLACK]
@black $(BLACK_FLAGS) $(PY_FILES)
- @echo [RUFF - PEP 585]
+ @echo [RUFF]
@ruff check --fix $(PY_FILES)
@echo [TYPECHECK]
@make -C core typecheck
### core/src/apps/bitcoin/sign_tx/approvers.py
@@ -13,7 +13,6 @@
if TYPE_CHECKING:
from buffer_types import AnyBytes
- from typing import Optional
from trezor.crypto import bip32
from trezor.messages import PaymentRequest, SignTx, TxInput, TxOutput
@@ -145,7 +144,7 @@ async def approve_tx(
self,
tx_info: TxInfo,
orig_txs: list[OriginalTxInfo],
- signer: Optional[Bitcoin],
+ signer: Bitcoin | None,
) -> None:
self.finish_payment_request()
@@ -327,7 +326,7 @@ async def approve_tx(
self,
tx_info: TxInfo,
orig_txs: list[OriginalTxInfo],
- signer: Optional[Bitcoin],
+ signer: Bitcoin | None,
) -> None:
from trezor.wire import NotEnoughFunds
@@ -513,7 +512,7 @@ async def approve_tx(
self,
tx_info: TxInfo,
orig_txs: list[OriginalTxInfo],
- signer: Optional[Bitcoin],
+ signer: Bitcoin | None,
) -> None:
from ..authorization import FEE_RATE_DECIMALS
### core/tools/analyze-memory-dump.py
@@ -6,7 +6,7 @@
import json
import sys
from collections.abc import Iterator
-from typing import Any, Optional, TypeGuard
+from typing import Any, TypeGuard
if len(sys.argv) < 2:
print("""\
@@ -126,7 +126,7 @@ def __getattr__(self, key: str) -> Any:
def find_modules(self) -> list["Item"]:
return [it for it in self.backlinks if it.type == "module"]
- def name(self) -> Optional[str]:
+ def name(self) -> str | None:
if "__name__" in self.dict:
return self.dict["__name__"]
### pyproject.toml
@@ -109,6 +109,15 @@ per-file-target-version = { "python/**/*.py" = "py39" }
[tool.ruff.lint]
select = [
+ # PEP 585
"UP006", # non-pep585-annotation: typing.Tuple -> tuple, typing.Type -> type, ...
"UP035", # deprecated-import: typing.Sequence -> collections.abc.Sequence, ...
+
+ # PEP 604 (used in /core only)
+ "UP007", # non-pep604-annotation-union: Union[int, str] -> int | str
+ "UP045", # non-pep604-annotation-optional: Optional[str] -> str | None
]
+
+[tool.ruff.lint.per-file-ignores]
+# PEP 604 syntax needs Python 3.10 - cannot be enforced e.g. in /python as it still supports Python 3.9.
+"!core/**/*.py" = ["UP007", "UP045"]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.