Skip to content

Commit 181bf70

Browse files
authored
Using ruff for linting #1 (#77)
* * remove black for linting * bump ruff version * add config for ruff rules (C only) * fix linting issues * format the codebase using ruff
1 parent af31439 commit 181bf70

31 files changed

+454
-914
lines changed

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ tasks:
99
lint:
1010
desc: Lints the code and reports on issues.
1111
cmds:
12-
- uv run black --check .
1312
- uv run ruff check .
13+
# - uv run ruff format . --check
1414

1515
build:
1616
desc: Builds the python package

pyproject.toml

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ dependencies = [
2525
"scipy==1.*",
2626
"numpy==1.*",
2727
"plotly==5.*",
28-
# "attrs==23.*"
2928
"psutil==5.*",
3029
"rich==13.*",
3130
"pandas==2.*",
@@ -43,8 +42,7 @@ dimred = "lasso.dimred.run:main"
4342
dev = [
4443
"pytest==8.*",
4544
"pytest-cov==5.*",
46-
"black==24.*",
47-
"ruff==0.3.*",
45+
"ruff==0.11.*",
4846
"mkdocs==1.*",
4947
"mkdocs-material==9.*",
5048
"mkdocstrings[python]==0.*",
@@ -58,10 +56,50 @@ dev = [
5856
requires = ["setuptools>=78", "setuptools-scm>=8"]
5957
build-backend = "setuptools.build_meta"
6058

61-
[tool.black]
62-
# We allow longer lines since 80 is quite short
63-
line-length=100
59+
[tool.ruff]
60+
required-version = "==0.11.*"
61+
line-length = 100
62+
indent-width = 4
63+
preview = true
6464

65-
[tool.flake8]
66-
exclude = [".git", "*migrations*"]
67-
max-line-length = 100
65+
# Output serialization format for violations. The default serialization
66+
# format is "full" [env: RUFF_OUTPUT_FORMAT=] [possible values:
67+
# concise, full, json, json-lines, junit, grouped, github, gitlab,
68+
# pylint, rdjson, azure, sarif]
69+
output-format = "grouped"
70+
71+
[tool.ruff.lint]
72+
isort.lines-after-imports = 2
73+
select = [
74+
"C", # Complexity checks (e.g., McCabe complexity, comprehensions)
75+
# "ANN001", "ANN201", "ANN401", # flake8-annotations (required strict type annotations for public functions)
76+
# "S", # flake8-bandit (checks basic security issues in code)
77+
# "BLE", # flake8-blind-except (checks the except blocks that do not specify exception)
78+
# "FBT", # flake8-boolean-trap (ensure that boolean args can be used with kw only)
79+
# "E", # pycodestyle errors (PEP 8 style guide violations)
80+
# "W", # pycodestyle warnings (e.g., extra spaces, indentation issues)
81+
# "DOC", # pydoclint issues (e.g., extra or missing return, yield, warnings)
82+
# "A", # flake8-buitins (check variable and function names to not shadow builtins)
83+
# "N", # Naming convention checks (e.g., PEP 8 variable and function names)
84+
# "F", # Pyflakes errors (e.g., unused imports, undefined variables)
85+
# "I", # isort (Ensures imports are sorted properly)
86+
# "B", # flake8-bugbear (Detects likely bugs and bad practices)
87+
# "TID", # flake8-tidy-imports (Checks for banned or misplaced imports)
88+
# "UP", # pyupgrade (Automatically updates old Python syntax)
89+
# "YTT", # flake8-2020 (Detects outdated Python 2/3 compatibility issues)
90+
# "FLY", # flynt (Converts old-style string formatting to f-strings)
91+
# "PIE", # flake8-pie
92+
# "PL", # pylint
93+
# "RUF", # Ruff-specific rules (Additional optimizations and best practices)
94+
]
95+
96+
ignore = [
97+
"PLR2004", # [magic-value-comparision](https://docs.astral.sh/ruff/rules/magic-value-comparison)
98+
"C90", # [mccabe](https://docs.astral.sh/ruff/rules/#mccabe-c90)
99+
]
100+
101+
[tool.ruff.lint.per-file-ignores]
102+
103+
[tool.ruff.format]
104+
docstring-code-format = true
105+
skip-magic-trailing-comma = true

src/lasso/diffcrash/diffcrash_run.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ def __init__(
287287
self.n_processes = self._parse_n_processes(n_processes)
288288

289289
def _setup_logger(self) -> logging.Logger:
290-
291290
# better safe than sorry
292291
os.makedirs(self.logfile_dir, exist_ok=True)
293292

@@ -310,7 +309,6 @@ def _setup_logger(self) -> logging.Logger:
310309
return logger
311310

312311
def _parse_diffcrash_home(self, diffcrash_home) -> str:
313-
314312
diffcrash_home_ok = len(diffcrash_home) != 0
315313

316314
msg = self._msg_option.format("diffcrash-home", diffcrash_home)
@@ -328,7 +326,6 @@ def _parse_diffcrash_home(self, diffcrash_home) -> str:
328326
return diffcrash_home
329327

330328
def _parse_crash_code(self, crash_code) -> str:
331-
332329
# these guys are allowed
333330
valid_crash_codes = ["dyna", "radioss", "pam"]
334331

@@ -340,16 +337,14 @@ def _parse_crash_code(self, crash_code) -> str:
340337

341338
if not crash_code_ok:
342339
err_msg = (
343-
f"Invalid crash code '{crash_code}'. "
344-
f"Please use one of: {str(valid_crash_codes)}"
340+
f"Invalid crash code '{crash_code}'. Please use one of: {str(valid_crash_codes)}"
345341
)
346342
self.logger.error(err_msg)
347343
raise RuntimeError(str_error(err_msg))
348344

349345
return crash_code
350346

351347
def _parse_reference_run(self, reference_run) -> str:
352-
353348
reference_run_ok = Path(reference_run).is_file()
354349

355350
msg = self._msg_option.format("reference-run", reference_run)
@@ -364,7 +359,6 @@ def _parse_reference_run(self, reference_run) -> str:
364359
return reference_run
365360

366361
def _parse_use_id_mapping(self, use_id_mapping) -> bool:
367-
368362
msg = self._msg_option.format("use-id-mapping", use_id_mapping)
369363
print(str_info(msg))
370364
self.logger.info(msg)
@@ -386,7 +380,6 @@ def _parse_simulation_runs(
386380
reference_run: str,
387381
exclude_runs: typing.Sequence[str],
388382
):
389-
390383
# search all denoted runs
391384
simulation_runs = []
392385
for pattern in simulation_run_patterns:
@@ -442,19 +435,16 @@ def natural_keys(text):
442435
return simulation_runs
443436

444437
def _parse_config_file(self, config_file) -> Union[str, None]:
445-
446438
_msg_config_file = ""
447439
if len(config_file) > 0 and not Path(config_file).is_file():
448440
config_file = None
449441
_msg_config_file = f"Can not find config file '{config_file}'"
450442

451443
# missing config file
452444
else:
453-
454445
config_file = None
455446
_msg_config_file = (
456-
"Config file missing. "
457-
"Consider specifying the path with the option '--config-file'."
447+
"Config file missing. Consider specifying the path with the option '--config-file'."
458448
)
459449

460450
msg = self._msg_option.format("config-file", config_file)
@@ -468,7 +458,6 @@ def _parse_config_file(self, config_file) -> Union[str, None]:
468458
return config_file
469459

470460
def _parse_parameter_file(self, parameter_file) -> Union[None, str]:
471-
472461
_msg_parameter_file = ""
473462
if len(parameter_file) > 0 and not Path(parameter_file).is_file():
474463
parameter_file = None
@@ -492,7 +481,6 @@ def _parse_parameter_file(self, parameter_file) -> Union[None, str]:
492481
return parameter_file
493482

494483
def _parse_n_processes(self, n_processes) -> int:
495-
496484
print(str_info(self._msg_option.format("n-processes", n_processes)))
497485

498486
if n_processes <= 0:
@@ -640,7 +628,6 @@ def run_import(self, pool: futures.ThreadPoolExecutor):
640628
# entry 0 is the reference run, thus we start at 1
641629
# pylint: disable = consider-using-enumerate
642630
for i_filepath in range(len(self.simulation_runs)):
643-
644631
# parameter file missing
645632
if self.parameter_file is None:
646633
if self.use_id_mapping:
@@ -691,7 +678,6 @@ def run_import(self, pool: futures.ThreadPoolExecutor):
691678
return_code_future.done() for return_code_future in return_code_futures
692679
)
693680
while n_imports_finished != len(return_code_futures):
694-
695681
# check again
696682
n_new_imports_finished = sum(
697683
return_code_future.done() for return_code_future in return_code_futures
@@ -717,7 +703,6 @@ def run_import(self, pool: futures.ThreadPoolExecutor):
717703

718704
# print failure
719705
if any(return_code != 0 for return_code in return_codes):
720-
721706
n_failed_runs = 0
722707
for i_run, return_code in enumerate(return_codes):
723708
if return_code != 0:
@@ -739,7 +724,6 @@ def run_import(self, pool: futures.ThreadPoolExecutor):
739724
# check log files
740725
messages = self.check_if_logfiles_show_success("DFC_Import_*.log")
741726
if messages:
742-
743727
# print failure
744728
msg = f"Running Imports ... done in {time.time() - start_time:.2f}s "
745729
print(str_error(msg))
@@ -774,14 +758,12 @@ def run_math(self, pool: futures.ThreadPoolExecutor):
774758

775759
start_time = time.time()
776760
return_code_future = pool.submit(
777-
run_subprocess,
778-
[self.diffcrash_home / f"DFC_Math_{self.crash_code}", self.project_dir],
761+
run_subprocess, [self.diffcrash_home / f"DFC_Math_{self.crash_code}", self.project_dir]
779762
)
780763
return_code = return_code_future.result()
781764

782765
# check return code
783766
if return_code != 0:
784-
785767
msg = f"Running Math ... done in {time.time() - start_time:.2f}s "
786768
print(str_error(msg))
787769
self.logger.error(msg)
@@ -793,7 +775,6 @@ def run_math(self, pool: futures.ThreadPoolExecutor):
793775
# check logs
794776
messages = self.check_if_logfiles_show_success("DFC_MATH*.log")
795777
if messages:
796-
797778
# print failure
798779
msg = f"Running Math ... done in {time.time() - start_time:.2f}s "
799780
print(str_error(msg))
@@ -889,7 +870,6 @@ def run_export(self, pool: futures.ThreadPoolExecutor):
889870
# check logs
890871
messages = self.check_if_logfiles_show_success("DFC_Export_*")
891872
if messages:
892-
893873
# print failure
894874
msg = f"Running Export ... done in {time.time() - start_time:.2f}s "
895875
print(str_error(msg))
@@ -900,10 +880,7 @@ def run_export(self, pool: futures.ThreadPoolExecutor):
900880
print(str_error(msg))
901881
self.logger.error(msg)
902882

903-
msg = (
904-
"At least one export failed. "
905-
f"Please check the log files in '{self.logfile_dir}'."
906-
)
883+
msg = f"At least one export failed. Please check the log files in '{self.logfile_dir}'."
907884
self.logger.error(msg)
908885
raise RuntimeError(str_error(msg))
909886

@@ -945,7 +922,6 @@ def run_matrix(self, pool: futures.ThreadPoolExecutor):
945922

946923
# check return code
947924
if return_code != 0:
948-
949925
# print failure
950926
msg = f"Running Matrix ... done in {time.time() - start_time:.2f}s "
951927
print(str_error(msg))
@@ -958,7 +934,6 @@ def run_matrix(self, pool: futures.ThreadPoolExecutor):
958934
# check log file
959935
messages = self.check_if_logfiles_show_success("DFC_Matrix_*")
960936
if messages:
961-
962937
# print failure
963938
msg = f"Running Matrix ... done in {time.time() - start_time:.2f}s "
964939
print(str_error(msg))
@@ -1021,7 +996,6 @@ def run_eigen(self, pool: futures.ThreadPoolExecutor):
1021996
# check log file
1022997
messages = self.check_if_logfiles_show_success("DFC_Matrix_*")
1023998
if messages:
1024-
1025999
# print failure
10261000
msg = f"Running Eigen ... done in {time.time() - start_time:.2f}s "
10271001
print(str_error(msg))

src/lasso/diffcrash/run.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818

1919
def _parse_stages(start_stage: str, end_stage: str):
20-
2120
# check validity
2221
if start_stage not in DC_STAGES or end_stage not in DC_STAGES:
2322
raise ValueError(
@@ -76,7 +75,6 @@ def main():
7675

7776
# initiate threading pool for handling jobs
7877
with futures.ThreadPoolExecutor(max_workers=diffcrash_run.n_processes) as pool:
79-
8078
# setup
8179
if start_stage_index <= DC_STAGES.index(DC_STAGE_SETUP) <= end_stage_index:
8280
diffcrash_run.run_setup(pool)

0 commit comments

Comments
 (0)