diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f12dda006..1961ec0c4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -46,7 +46,7 @@ repos: - '' - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.8.6 + rev: v0.9.0 hooks: - id: ruff name: Run Ruff linter diff --git a/anta/catalog.py b/anta/catalog.py index 65031cedc..ca0eeb176 100644 --- a/anta/catalog.py +++ b/anta/catalog.py @@ -182,7 +182,7 @@ def flatten_modules(data: dict[str, Any], package: str | None = None) -> dict[Mo except Exception as e: # A test module is potentially user-defined code. # We need to catch everything if we want to have meaningful logs - module_str = f"{module_name[1:] if module_name.startswith('.') else module_name}{f' from package {package}' if package else ''}" + module_str = f"{module_name.removeprefix('.')}{f' from package {package}' if package else ''}" message = f"Module named {module_str} cannot be imported. Verify that the module exists and there is no Python syntax issues." anta_log_exception(e, message, logger) raise ValueError(message) from e @@ -223,16 +223,14 @@ def check_tests(cls: type[AntaCatalogFile], data: Any) -> Any: # noqa: ANN401 raise ValueError(msg) # noqa: TRY004 pydantic catches ValueError or AssertionError, no TypeError if len(test_definition) != 1: msg = ( - f"Syntax error when parsing: {test_definition}\n" - "It must be a dictionary with a single entry. Check the indentation in the test catalog." + f"Syntax error when parsing: {test_definition}\nIt must be a dictionary with a single entry. Check the indentation in the test catalog." ) raise ValueError(msg) for test_name, test_inputs in test_definition.copy().items(): test: type[AntaTest] | None = getattr(module, test_name, None) if test is None: msg = ( - f"{test_name} is not defined in Python module {module.__name__}" - f"{f' (from {module.__file__})' if module.__file__ is not None else ''}" + f"{test_name} is not defined in Python module {module.__name__}{f' (from {module.__file__})' if module.__file__ is not None else ''}" ) raise ValueError(msg) test_definitions.append(AntaTestDefinition(test=test, inputs=test_inputs)) diff --git a/anta/runner.py b/anta/runner.py index 93c433722..2b3c8b70e 100644 --- a/anta/runner.py +++ b/anta/runner.py @@ -115,7 +115,7 @@ async def setup_inventory(inventory: AntaInventory, tags: set[str] | None, devic # If there are no devices in the inventory after filtering, exit if not selected_inventory.devices: - msg = f'No reachable device {f"matching the tags {tags} " if tags else ""}was found.{f" Selected devices: {devices} " if devices is not None else ""}' + msg = f"No reachable device {f'matching the tags {tags} ' if tags else ''}was found.{f' Selected devices: {devices} ' if devices is not None else ''}" logger.warning(msg) return None @@ -170,8 +170,7 @@ def prepare_tests( if total_test_count == 0: msg = ( - f"There are no tests{f' matching the tags {tags} ' if tags else ' '}to run in the current " - "test catalog and device inventory, please verify your inputs." + f"There are no tests{f' matching the tags {tags} ' if tags else ' '}to run in the current test catalog and device inventory, please verify your inputs." ) logger.warning(msg) return None diff --git a/anta/tests/system.py b/anta/tests/system.py index 85235f2fc..11cf8398c 100644 --- a/anta/tests/system.py +++ b/anta/tests/system.py @@ -224,7 +224,7 @@ def test(self) -> None: if memory_usage > MEMORY_THRESHOLD: self.result.is_success() else: - self.result.is_failure(f"Device has reported a high memory usage: {(1 - memory_usage)*100:.2f}%") + self.result.is_failure(f"Device has reported a high memory usage: {(1 - memory_usage) * 100:.2f}%") class VerifyFileSystemUtilization(AntaTest): diff --git a/pyproject.toml b/pyproject.toml index 1e85b01f0..09fee8c3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -75,7 +75,7 @@ dev = [ "pytest-metadata>=3.0.0", "pytest>=7.4.0", "respx>=0.22.0", - "ruff>=0.5.4,<0.9.0", + "ruff>=0.5.4,<0.10.0", "tox>=4.10.0,<5.0.0", "types-PyYAML", "types-pyOpenSSL", @@ -428,6 +428,9 @@ runtime-evaluated-base-classes = ["pydantic.BaseModel", "anta.models.AntaTest.In "C901", # TODO: test function is too complex, needs a refactor "PLR0912" # Too many branches (15/12) (too-many-branches), needs a refactor ] +"anta/tests/logging.py" = [ + "A005", # TODO: Module `logging` shadows a Python standard-library module +] "anta/decorators.py" = [ "ANN401", # Ok to use Any type hint in our decorators ] diff --git a/tests/benchmark/test_anta.py b/tests/benchmark/test_anta.py index 91d3b3ff2..1daf7f369 100644 --- a/tests/benchmark/test_anta.py +++ b/tests/benchmark/test_anta.py @@ -47,7 +47,7 @@ def _() -> None: if len(results.results) != len(inventory) * len(catalog.tests): pytest.fail(f"Expected {len(inventory) * len(catalog.tests)} tests but got {len(results.results)}", pytrace=False) - bench_info = "\n--- ANTA NRFU Dry-Run Benchmark Information ---\n" f"Test count: {len(results.results)}\n" "-----------------------------------------------" + bench_info = f"\n--- ANTA NRFU Dry-Run Benchmark Information ---\nTest count: {len(results.results)}\n-----------------------------------------------" logger.info(bench_info) diff --git a/tests/units/test_runner.py b/tests/units/test_runner.py index b3ac1b56a..1b9c40c88 100644 --- a/tests/units/test_runner.py +++ b/tests/units/test_runner.py @@ -67,7 +67,7 @@ async def test_no_selected_device(caplog: pytest.LogCaptureFixture, inventory: A caplog.set_level(logging.WARNING) manager = ResultManager() await main(manager, inventory, FAKE_CATALOG, tags=tags, devices=devices) - msg = f'No reachable device {f"matching the tags {tags} " if tags else ""}was found.{f" Selected devices: {devices} " if devices is not None else ""}' + msg = f"No reachable device {f'matching the tags {tags} ' if tags else ''}was found.{f' Selected devices: {devices} ' if devices is not None else ''}" assert msg in caplog.messages