From e85e9e89988e5d0a80cdf65828a9f7a12d8e15c9 Mon Sep 17 00:00:00 2001 From: grdddj Date: Mon, 14 Aug 2023 09:33:55 +0200 Subject: [PATCH] chore(python): add ruff linter as a dependency [no changelog] --- ci/shell.nix | 1 + core/embed/firmware/bootloader_hashes.py | 10 +++++++--- core/prof/prof.py | 6 +++--- core/tools/alloc.py | 2 +- core/tools/hid-bridge/logger.py | 2 +- pyproject.toml | 8 ++++++++ 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ci/shell.nix b/ci/shell.nix index eaa0fb0ad6..514beb9177 100644 --- a/ci/shell.nix +++ b/ci/shell.nix @@ -98,6 +98,7 @@ stdenvNoCC.mkDerivation ({ poetry protobuf3_19 pyright + ruff (mkBinOnlyWrapper rustNightly) wget zlib diff --git a/core/embed/firmware/bootloader_hashes.py b/core/embed/firmware/bootloader_hashes.py index 1dd79bdf24..53a02aa55c 100755 --- a/core/embed/firmware/bootloader_hashes.py +++ b/core/embed/firmware/bootloader_hashes.py @@ -29,7 +29,7 @@ def aligned_digest(data: bytes, padding: bytes) -> bytes: (unwritten NOR-flash byte) or 0x00 (explicitly cleared byte). """ if len(data) > ALIGNED_SIZE: - raise ValueError(fn, "too big") + raise ValueError("too big") assert len(padding) == 1 digest_data = data + padding * (ALIGNED_SIZE - len(data)) @@ -55,8 +55,12 @@ def bootloader_str(file: Path) -> str: data = file.read_bytes() suffix = file.stem[len("bootloader_") :].upper() - bytes_00 = to_uint_array(aligned_digest(data, b"\x00")) - bytes_ff = to_uint_array(aligned_digest(data, b"\xff")) + + try: + bytes_00 = to_uint_array(aligned_digest(data, b"\x00")) + bytes_ff = to_uint_array(aligned_digest(data, b"\xff")) + except ValueError: + raise ValueError(f"Data too big in {file} - {len(data)} bytes") try: bl = FirmwareImage.parse(data) diff --git a/core/prof/prof.py b/core/prof/prof.py index 2ddf299390..32bd909664 100644 --- a/core/prof/prof.py +++ b/core/prof/prof.py @@ -16,7 +16,7 @@ def __init__(self): self.__files = {} def line_tick(self, filename, lineno): - if not filename in self.__files: + if filename not in self.__files: self.__files[filename] = set() self.__files[filename].add(lineno) @@ -120,7 +120,7 @@ def atexit(): sys.atexit(atexit) global __prof__ -if not "__prof__" in globals(): +if "__prof__" not in globals(): if getenv("TREZOR_MEMPERF") == "1": __prof__ = AllocCounter() else: @@ -131,4 +131,4 @@ def atexit(): if isinstance(__prof__, AllocCounter): __prof__.last_alloc_count = micropython.alloc_count() -import main +import main # noqa: F401 (imported but unused) diff --git a/core/tools/alloc.py b/core/tools/alloc.py index 32ccc79729..4e5559ee39 100755 --- a/core/tools/alloc.py +++ b/core/tools/alloc.py @@ -10,7 +10,7 @@ import click from dominate import document -from dominate.tags import * +from dominate.tags import meta, style, h3, table, thead, tr, th, tbody, td, a from dominate.util import raw HERE = Path(__file__).resolve().parent diff --git a/core/tools/hid-bridge/logger.py b/core/tools/hid-bridge/logger.py index d6d6732d47..09394f76f4 100644 --- a/core/tools/hid-bridge/logger.py +++ b/core/tools/hid-bridge/logger.py @@ -9,7 +9,7 @@ def __get_timestamp(): def __log_message(message): - if log_timestamps == True: + if log_timestamps is True: print(f"{__get_timestamp()}\t{message}") else: print(message) diff --git a/pyproject.toml b/pyproject.toml index 698bb75bcc..2dbb6ca057 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -102,3 +102,11 @@ extra_standard_library = [ ] known_first_party = ["trezorlib", "apps", "coin_info", "marketcap", "ui_tests", "gitlab"] known_third_party = ["trezor", "storage"] + +[tool.ruff] +ignore = [ + "E501", # line too long + "E402", # module level import not at top of file + "E741", # ambiguous variable name +] +exclude = ["tests", "mocks"]