Skip to content

Commit c5e8664

Browse files
fundakolcarlescufi
authored andcommitted
twister: Refactor python module to follow PEP8 rules
Incremental refactoring. Fix PEP8 issues to make ruff green for config_parser.py module. Add __init__.py to twister directory to make it a proper python package, and make modules importable. Signed-off-by: Lukasz Fundakowski <[email protected]>
1 parent 27a746f commit c5e8664

File tree

5 files changed

+65
-64
lines changed

5 files changed

+65
-64
lines changed

.ruff-excludes.toml

-8
Original file line numberDiff line numberDiff line change
@@ -828,14 +828,6 @@
828828
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
829829
"UP026", # https://docs.astral.sh/ruff/rules/deprecated-mock-import
830830
]
831-
"./scripts/tests/twister/test_config_parser.py" = [
832-
"B017", # https://docs.astral.sh/ruff/rules/assert-raises-exception
833-
"B033", # https://docs.astral.sh/ruff/rules/duplicate-value
834-
"E501", # https://docs.astral.sh/ruff/rules/line-too-long
835-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
836-
"SIM117", # https://docs.astral.sh/ruff/rules/multiple-with-statements
837-
"UP026", # https://docs.astral.sh/ruff/rules/deprecated-mock-import
838-
]
839831
"./scripts/tests/twister/test_data/mixins/test_to_ignore.py" = [
840832
"B011", # https://docs.astral.sh/ruff/rules/assert-false
841833
]

scripts/pylib/twister/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0

scripts/pylib/twister/twisterlib/config_parser.py

+61-56
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55

66
import copy
77
import warnings
8+
from typing import Any
89

910
import scl
1011
from twisterlib.error import ConfigurationError
1112

1213

13-
def extract_fields_from_arg_list(target_fields: set, arg_list: str | list):
14+
def extract_fields_from_arg_list(
15+
target_fields: set, arg_list: str | list
16+
) -> tuple[dict[str, list[str]], list[str]]:
1417
"""
1518
Given a list of "FIELD=VALUE" args, extract values of args with a
1619
given field name and return the remaining args separately.
1720
"""
18-
extracted_fields = {f : list() for f in target_fields}
19-
other_fields = []
21+
extracted_fields: dict[str, list[str]] = {f: list() for f in target_fields}
22+
other_fields: list[str] = []
2023

2124
if isinstance(arg_list, str):
2225
args = arg_list.strip().split()
@@ -39,64 +42,66 @@ def extract_fields_from_arg_list(target_fields: set, arg_list: str | list):
3942

4043
return extracted_fields, other_fields
4144

45+
4246
class TwisterConfigParser:
4347
"""Class to read testsuite yaml files with semantic checking
4448
"""
4549

46-
testsuite_valid_keys = {"tags": {"type": "set", "required": False},
47-
"type": {"type": "str", "default": "integration"},
48-
"extra_args": {"type": "list"},
49-
"extra_configs": {"type": "list"},
50-
"extra_conf_files": {"type": "list", "default": []},
51-
"extra_overlay_confs" : {"type": "list", "default": []},
52-
"extra_dtc_overlay_files": {"type": "list", "default": []},
53-
"required_snippets": {"type": "list"},
54-
"build_only": {"type": "bool", "default": False},
55-
"build_on_all": {"type": "bool", "default": False},
56-
"skip": {"type": "bool", "default": False},
57-
"slow": {"type": "bool", "default": False},
58-
"timeout": {"type": "int", "default": 60},
59-
"min_ram": {"type": "int", "default": 16},
60-
"modules": {"type": "list", "default": []},
61-
"depends_on": {"type": "set"},
62-
"min_flash": {"type": "int", "default": 32},
63-
"arch_allow": {"type": "set"},
64-
"arch_exclude": {"type": "set"},
65-
"vendor_allow": {"type": "set"},
66-
"vendor_exclude": {"type": "set"},
67-
"extra_sections": {"type": "list", "default": []},
68-
"integration_platforms": {"type": "list", "default": []},
69-
"integration_toolchains": {"type": "list", "default": []},
70-
"ignore_faults": {"type": "bool", "default": False },
71-
"ignore_qemu_crash": {"type": "bool", "default": False },
72-
"testcases": {"type": "list", "default": []},
73-
"platform_type": {"type": "list", "default": []},
74-
"platform_exclude": {"type": "set"},
75-
"platform_allow": {"type": "set"},
76-
"platform_key": {"type": "list", "default": []},
77-
"simulation_exclude": {"type": "list", "default": []},
78-
"toolchain_exclude": {"type": "set"},
79-
"toolchain_allow": {"type": "set"},
80-
"filter": {"type": "str"},
81-
"levels": {"type": "list", "default": []},
82-
"harness": {"type": "str", "default": "test"},
83-
"harness_config": {"type": "map", "default": {}},
84-
"seed": {"type": "int", "default": 0},
85-
"sysbuild": {"type": "bool", "default": False}
86-
}
87-
88-
def __init__(self, filename, schema):
50+
testsuite_valid_keys: dict[str, dict[str, Any]] = {
51+
"tags": {"type": "set", "required": False},
52+
"type": {"type": "str", "default": "integration"},
53+
"extra_args": {"type": "list"},
54+
"extra_configs": {"type": "list"},
55+
"extra_conf_files": {"type": "list", "default": []},
56+
"extra_overlay_confs": {"type": "list", "default": []},
57+
"extra_dtc_overlay_files": {"type": "list", "default": []},
58+
"required_snippets": {"type": "list"},
59+
"build_only": {"type": "bool", "default": False},
60+
"build_on_all": {"type": "bool", "default": False},
61+
"skip": {"type": "bool", "default": False},
62+
"slow": {"type": "bool", "default": False},
63+
"timeout": {"type": "int", "default": 60},
64+
"min_ram": {"type": "int", "default": 16},
65+
"modules": {"type": "list", "default": []},
66+
"depends_on": {"type": "set"},
67+
"min_flash": {"type": "int", "default": 32},
68+
"arch_allow": {"type": "set"},
69+
"arch_exclude": {"type": "set"},
70+
"vendor_allow": {"type": "set"},
71+
"vendor_exclude": {"type": "set"},
72+
"extra_sections": {"type": "list", "default": []},
73+
"integration_platforms": {"type": "list", "default": []},
74+
"integration_toolchains": {"type": "list", "default": []},
75+
"ignore_faults": {"type": "bool", "default": False},
76+
"ignore_qemu_crash": {"type": "bool", "default": False},
77+
"testcases": {"type": "list", "default": []},
78+
"platform_type": {"type": "list", "default": []},
79+
"platform_exclude": {"type": "set"},
80+
"platform_allow": {"type": "set"},
81+
"platform_key": {"type": "list", "default": []},
82+
"simulation_exclude": {"type": "list", "default": []},
83+
"toolchain_exclude": {"type": "set"},
84+
"toolchain_allow": {"type": "set"},
85+
"filter": {"type": "str"},
86+
"levels": {"type": "list", "default": []},
87+
"harness": {"type": "str", "default": "test"},
88+
"harness_config": {"type": "map", "default": {}},
89+
"seed": {"type": "int", "default": 0},
90+
"sysbuild": {"type": "bool", "default": False}
91+
}
92+
93+
def __init__(self, filename: str, schema: dict[str, Any]) -> None:
8994
"""Instantiate a new TwisterConfigParser object
9095
9196
@param filename Source .yaml file to read
9297
"""
93-
self.data = {}
9498
self.schema = schema
9599
self.filename = filename
96-
self.scenarios = {}
97-
self.common = {}
100+
self.data: dict[str, Any] = {}
101+
self.scenarios: dict[str, Any] = {}
102+
self.common: dict[str, Any] = {}
98103

99-
def load(self):
104+
def load(self) -> dict[str, Any]:
100105
data = scl.yaml_load_verify(self.filename, self.schema)
101106
self.data = data
102107

@@ -106,7 +111,7 @@ def load(self):
106111
self.common = self.data['common']
107112
return data
108113

109-
def _cast_value(self, value, typestr):
114+
def _cast_value(self, value: Any, typestr: str) -> Any:
110115
if typestr == "str":
111116
return value.strip()
112117

@@ -142,7 +147,7 @@ def _cast_value(self, value, typestr):
142147
else:
143148
raise ConfigurationError(self.filename, f"unknown type '{value}'")
144149

145-
def get_scenario(self, name):
150+
def get_scenario(self, name: str) -> dict[str, Any]:
146151
"""Get a dictionary representing the keys/values within a scenario
147152
148153
@param name The scenario in the .yaml file to retrieve data from
@@ -152,10 +157,10 @@ def get_scenario(self, name):
152157

153158
# "CONF_FILE", "OVERLAY_CONFIG", and "DTC_OVERLAY_FILE" fields from each
154159
# of the extra_args lines
155-
extracted_common = {}
156-
extracted_testsuite = {}
160+
extracted_common: dict = {}
161+
extracted_testsuite: dict = {}
157162

158-
d = {}
163+
d: dict[str, Any] = {}
159164
for k, v in self.common.items():
160165
if k == "extra_args":
161166
# Pull out these fields and leave the rest
@@ -223,7 +228,7 @@ def get_scenario(self, name):
223228
self.scenarios[name].get("extra_dtc_overlay_files", [])
224229

225230
if any({len(x) > 0 for x in extracted_common.values()}) or \
226-
any({len(x) > 0 for x in extracted_testsuite.values()}):
231+
any({len(x) > 0 for x in extracted_testsuite.values()}):
227232
warnings.warn(
228233
"Do not specify CONF_FILE, OVERLAY_CONFIG, or DTC_OVERLAY_FILE "
229234
"in extra_args. This feature is deprecated and will soon "

scripts/pylib/twister/twisterlib/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)