From abeab83f0cecd8c4b6175f8546a4ada84833a6b1 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 10:59:28 +0200 Subject: [PATCH 01/14] Introduce the Ruff linter and formatter Ruff is a modern linter and formatter in one. --- .github/workflows/ci.yml | 3 ++ pyproject.toml | 79 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a3a6bb1..2c1477c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,5 +27,8 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install . + python -m pip install ruff + - name: Lint + run: python -m ruff check . - name: Test with python unittest run: python -m unittest diff --git a/pyproject.toml b/pyproject.toml index bd4b8a5..9fe2e6a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,3 +51,82 @@ include = ["trsfile", "trsfile.*"] [tool.setuptools_scm] # This writes the computed version into the package at build time write_to = "trsfile/VERSION.txt" + +[tool.ruff] +# Exclude a variety of commonly ignored directories. +exclude = [ + ".bzr", + ".direnv", + ".eggs", + ".git", + ".git-rewrite", + ".hg", + ".ipynb_checkpoints", + ".mypy_cache", + ".nox", + ".pants.d", + ".pyenv", + ".pytest_cache", + ".pytype", + ".ruff_cache", + ".svn", + ".tox", + ".venv", + ".vscode", + "__pypackages__", + "_build", + "buck-out", + "build", + "dist", + "node_modules", + "site-packages", + "venv", +] + +# Same as Black. +line-length = 88 +indent-width = 4 + +# Assume Python 3.10 +target-version = "py310" + +[tool.ruff.lint] +# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default. +# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or +# McCabe complexity (`C901`) by default. +select = ["E4", "E7", "E9", "F"] +ignore = [] + +# Allow fix for all enabled rules (when `--fix`) is provided. +fixable = ["ALL"] +unfixable = [] + +# Allow unused variables when underscore-prefixed. +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" + +[tool.ruff.format] +# Like Black, use double quotes for strings. +quote-style = "double" + +# Like Black, indent with spaces, rather than tabs. +indent-style = "space" + +# Like Black, respect magic trailing commas. +skip-magic-trailing-comma = false + +# Like Black, automatically detect the appropriate line ending. +line-ending = "auto" + +# Enable auto-formatting of code examples in docstrings. Markdown, +# reStructuredText code/literal blocks and doctests are all supported. +# +# This is currently disabled by default, but it is planned for this +# to be opt-out in the future. +docstring-code-format = false + +# Set the line length limit used when formatting code snippets in +# docstrings. +# +# This only has an effect when the `docstring-code-format` setting is +# enabled. +docstring-code-line-length = "dynamic" \ No newline at end of file From a2da9ae8da9c582fd4993b5903fbe6cdc48c182f Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 11:52:38 +0200 Subject: [PATCH 02/14] Fix F811 error --- tests/test_creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_creation.py b/tests/test_creation.py index ff0769a..2c59b4c 100644 --- a/tests/test_creation.py +++ b/tests/test_creation.py @@ -210,7 +210,7 @@ def test_write_closed(self): with self.assertRaises(ValueError): print(trs_traces) - def test_write_different_trace_sizes(self): + def test_write_different_trace_sizes2(self): trace_count = 100 sample_count = 1000 From 7adcdfcf6737de7575ce9cc245e9926ee4f17d8f Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 11:53:37 +0200 Subject: [PATCH 03/14] Fix F841 error --- tests/test_creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_creation.py b/tests/test_creation.py index 2c59b4c..3e188e3 100644 --- a/tests/test_creation.py +++ b/tests/test_creation.py @@ -316,7 +316,7 @@ def test_read(self): def test_read_non_existing(self): with self.assertRaises(FileNotFoundError): - with trsfile.open(self.tmp_path, 'r') as trs_traces: + with trsfile.open(self.tmp_path, 'r'): pass def test_append(self): From 1c9df1994f35cc53259bf2826264ebb28ca7a7bb Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 11:59:06 +0200 Subject: [PATCH 04/14] Fix F405 error --- tests/test_parametermap.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/tests/test_parametermap.py b/tests/test_parametermap.py index 6e184c5..4416cf3 100644 --- a/tests/test_parametermap.py +++ b/tests/test_parametermap.py @@ -1,8 +1,28 @@ from unittest import TestCase -from trsfile.parametermap import TraceSetParameterMap, TraceParameterDefinitionMap, TraceParameterMap, RawTraceData -from trsfile.standardparameters import StandardTraceSetParameters, StandardTraceParameters -from trsfile.traceparameter import * +from trsfile.parametermap import ( + RawTraceData, + TraceParameterDefinitionMap, + TraceParameterMap, + TraceSetParameterMap, +) +from trsfile.standardparameters import ( + StandardTraceParameters, + StandardTraceSetParameters, +) +from trsfile.traceparameter import ( + BooleanArrayParameter, + ByteArrayParameter, + DoubleArrayParameter, + FloatArrayParameter, + IntegerArrayParameter, + LongArrayParameter, + ParameterType, + ShortArrayParameter, + StringParameter, + TraceParameterDefinition, +) +from io import BytesIO class TestTraceSetParameterMap(TestCase): From 11a60d1e5c7f75d54f740f1c4fd077dcc660e65b Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 12:00:32 +0200 Subject: [PATCH 05/14] Fix F841 error --- tests/test_trsfile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_trsfile.py b/tests/test_trsfile.py index abed420..5a95885 100644 --- a/tests/test_trsfile.py +++ b/tests/test_trsfile.py @@ -32,10 +32,10 @@ def test_iterator(self): def test_read_only(self): """Check if the TrsFile is read only""" - with self.assertRaises(TypeError) as cm: + with self.assertRaises(TypeError): self.trs_file[0] = None - with self.assertRaises(TypeError) as cm: + with self.assertRaises(TypeError): del self.trs_file[0] def test_slice(self): From 188e17b8441aa4e52e78263019ec528bb655a4f9 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 12:01:48 +0200 Subject: [PATCH 06/14] Fix E721 error --- trsfile/compatibility.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trsfile/compatibility.py b/trsfile/compatibility.py index 72490bd..6a1de2b 100644 --- a/trsfile/compatibility.py +++ b/trsfile/compatibility.py @@ -10,7 +10,7 @@ def __init__(self, *aliases): self.aliases = set(aliases) def __call__(self, obj): - if type(obj) == property: + if isinstance(obj, property): obj.fget._aliases = self.aliases else: obj._aliases = self.aliases @@ -34,7 +34,7 @@ def inner(*args, **kwargs): for name, method in aliased_class_dict.items(): aliases = None - if (type(method) == property) and hasattr(method.fget, '_aliases'): + if isinstance(method, property) and hasattr(method.fget, '_aliases'): aliases = method.fget._aliases elif hasattr(method, '_aliases'): aliases = method._aliases From d759d5536712ebee6b85acb674b4b9df1c1ac92c Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 12:02:54 +0200 Subject: [PATCH 07/14] Fix F401 warning --- trsfile/converters/chipwhisperer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trsfile/converters/chipwhisperer.py b/trsfile/converters/chipwhisperer.py index 4774ec6..25a409e 100644 --- a/trsfile/converters/chipwhisperer.py +++ b/trsfile/converters/chipwhisperer.py @@ -7,8 +7,8 @@ from chipwhisperer.common.api.ProjectFormat import Project from trsfile import TraceSet, Header, TracePadding, Trace, SampleCoding -from trsfile.parametermap import TraceSetParameterMap, TraceParameterDefinitionMap, TraceParameterMap -from trsfile.traceparameter import TraceParameterDefinition, ParameterType, StringParameter, ByteArrayParameter +from trsfile.parametermap import TraceSetParameterMap, TraceParameterMap +from trsfile.traceparameter import StringParameter, ByteArrayParameter def to_trs(path_to_project: str, output_path: str, trace_index: int = 0): From 159626fe4a403cc36d00aec98c87a18190a3cd03 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 12:04:17 +0200 Subject: [PATCH 08/14] Fix E722 error --- trsfile/converters/chipwhisperer.py | 4 ++-- trsfile/engine/file.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/trsfile/converters/chipwhisperer.py b/trsfile/converters/chipwhisperer.py index 25a409e..75e7006 100644 --- a/trsfile/converters/chipwhisperer.py +++ b/trsfile/converters/chipwhisperer.py @@ -37,7 +37,7 @@ def to_trs(path_to_project: str, output_path: str, trace_index: int = 0): extra_parameters = CWSettings.read( os.path.join(trace_folder, container.config.attr('prefix') + 'settings.cwset')) traceset_parameters.update(extra_parameters) - except: + except Exception: print('Warning: Failed to read additional settings. Trace reading will continue without additional settings.') with TraceSet(path=output_path, @@ -58,7 +58,7 @@ def to_trs(path_to_project: str, output_path: str, trace_index: int = 0): def read_or_default(config, attr: str, default: Any = ''): try: return config.attr(attr) - except: + except Exception: return default diff --git a/trsfile/engine/file.py b/trsfile/engine/file.py index 7d76f3d..d5f888d 100644 --- a/trsfile/engine/file.py +++ b/trsfile/engine/file.py @@ -75,7 +75,7 @@ def __init__(self, path, mode = 'x', **options): try: while self.path.is_dir(): time.sleep(0.001) - except: + except Exception: pass # Create the temporary folder and initialize this class From e304a533c35ebb2d1670346bccf54f8b0123f7e4 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 12:05:32 +0200 Subject: [PATCH 09/14] Fix E713 warning --- trsfile/engine/file.py | 2 +- trsfile/engine/trs.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/trsfile/engine/file.py b/trsfile/engine/file.py index d5f888d..d2ffbe5 100644 --- a/trsfile/engine/file.py +++ b/trsfile/engine/file.py @@ -124,7 +124,7 @@ def __initialize_headers(self): # Add any mandatory headers that are missing for header in Header.get_mandatory(): - if not header in headers: + if header not in headers: headers[header] = header.default # Store these default headers diff --git a/trsfile/engine/trs.py b/trsfile/engine/trs.py index cad2cbb..55f6ff7 100644 --- a/trsfile/engine/trs.py +++ b/trsfile/engine/trs.py @@ -461,7 +461,7 @@ def __create_headers(self, headers: Optional[Dict[Header, Any]]): # Add any mandatory headers that are missing for header in Header.get_mandatory(): - if not header in self.headers: + if header not in self.headers: self.headers[header] = header.default # Make sure correct trs version is set From 525f432c329c7fd9e468b66455efd06bdb367eaf Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 12:08:17 +0200 Subject: [PATCH 10/14] Fix F403 warning --- trsfile/parametermap.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/trsfile/parametermap.py b/trsfile/parametermap.py index 969f5a2..81d623a 100644 --- a/trsfile/parametermap.py +++ b/trsfile/parametermap.py @@ -5,13 +5,32 @@ from typing import Any, Union, List, Dict from trsfile.compatibility import alias, aliased -from trsfile.standardparameters import StandardTraceSetParameters, StandardTraceParameters -from trsfile.traceparameter import TraceSetParameter, TraceParameter, TraceParameterDefinition, ParameterType, \ - BooleanArrayParameter, ByteArrayParameter, StringParameter, DoubleArrayParameter, IntegerArrayParameter, \ - LongArrayParameter, ShortArrayParameter -from trsfile.utils import * +from trsfile.standardparameters import ( + StandardTraceParameters, + StandardTraceSetParameters, +) +from trsfile.traceparameter import ( + BooleanArrayParameter, + ByteArrayParameter, + DoubleArrayParameter, + IntegerArrayParameter, + LongArrayParameter, + ParameterType, + ShortArrayParameter, + StringParameter, + TraceParameter, + TraceParameterDefinition, + TraceSetParameter, +) +from trsfile.utils import ( + encode_as_short, + read_parameter_name, + read_short, + StringKeyOrderedDict, + UTF_8, +) +from io import BytesIO -UTF_8 = 'utf-8' SHORT_MIN = -2**15 SHORT_MAX = 2**15-1 INT_MIN = -2**31 From bd2e10b600d640a5b8d8052b29f6b3d047b76699 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 12:11:23 +0200 Subject: [PATCH 11/14] Fix E721 error --- trsfile/parametermap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trsfile/parametermap.py b/trsfile/parametermap.py index 81d623a..31e5def 100644 --- a/trsfile/parametermap.py +++ b/trsfile/parametermap.py @@ -139,7 +139,7 @@ def get_typed_parameter(param_value: ParameterValueType) -> type: """Get the subclass of TraceParameter needed to hold a given value Throws an error if the value cannot be stored in any TraceParameter subclass""" value_type = ParameterMapUtil._get_type(param_value) - if value_type == list: + if value_type is list: value_type = ParameterMapUtil._get_type_of_list_elems(param_value) return ParameterMapUtil.TYPE_TO_PARAMETER[value_type] From 376040c85f5df4e76e6b4043223c0b34dcbb3224 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 12:14:49 +0200 Subject: [PATCH 12/14] Fix missing import --- trsfile/parametermap.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/trsfile/parametermap.py b/trsfile/parametermap.py index 31e5def..4be6eb7 100644 --- a/trsfile/parametermap.py +++ b/trsfile/parametermap.py @@ -5,6 +5,7 @@ from typing import Any, Union, List, Dict from trsfile.compatibility import alias, aliased +from trsfile.common import Header from trsfile.standardparameters import ( StandardTraceParameters, StandardTraceSetParameters, @@ -257,7 +258,7 @@ def add_standard_parameter(self, std_trace_set_param: StandardTraceSetParameters self[std_trace_set_param.identifier] = typed_param(ParameterMapUtil.to_list_if_listable(value)) return self - def fill_from_headers(self, headers: Dict['Header', Any]) -> TraceSetParameterMap: + def fill_from_headers(self, headers: Dict[Header, Any]) -> TraceSetParameterMap: """Add to this trace set parameter map all data that is in the header and for which standard trace set parameters exist. Data that already exists in the map will not be overwritten. From ec5a0ab25ccb8d45827701f8d7813b7e6e60d26d Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 12:15:29 +0200 Subject: [PATCH 13/14] Fix E714 warning --- trsfile/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trsfile/utils.py b/trsfile/utils.py index 3afc744..3fb0999 100644 --- a/trsfile/utils.py +++ b/trsfile/utils.py @@ -23,6 +23,6 @@ def read_short(io_bytes: BytesIO): class StringKeyOrderedDict(OrderedDict): def __setitem__(self, key, value): - if not type(key) is str: + if not isinstance(key, str): raise TypeError('The key for an item in a {} must be of type \'str\'.'.format(self.__class__.__name__)) super().__setitem__(key, value) From 8c79b7824d96f70e0adae81f10a2179031da1f3f Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 28 Apr 2026 12:26:18 +0200 Subject: [PATCH 14/14] Fix circular import error --- trsfile/parametermap.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/trsfile/parametermap.py b/trsfile/parametermap.py index 4be6eb7..215c1f7 100644 --- a/trsfile/parametermap.py +++ b/trsfile/parametermap.py @@ -2,10 +2,12 @@ import copy import numbers import warnings -from typing import Any, Union, List, Dict +from typing import Any, Union, List, Dict, TYPE_CHECKING + +if TYPE_CHECKING: + from trsfile.common import Header from trsfile.compatibility import alias, aliased -from trsfile.common import Header from trsfile.standardparameters import ( StandardTraceParameters, StandardTraceSetParameters, @@ -258,7 +260,7 @@ def add_standard_parameter(self, std_trace_set_param: StandardTraceSetParameters self[std_trace_set_param.identifier] = typed_param(ParameterMapUtil.to_list_if_listable(value)) return self - def fill_from_headers(self, headers: Dict[Header, Any]) -> TraceSetParameterMap: + def fill_from_headers(self, headers: Dict["Header", Any]) -> TraceSetParameterMap: """Add to this trace set parameter map all data that is in the header and for which standard trace set parameters exist. Data that already exists in the map will not be overwritten.