summaryrefslogtreecommitdiffstats
path: root/anta/decorators.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--anta/decorators.py54
1 files changed, 31 insertions, 23 deletions
diff --git a/anta/decorators.py b/anta/decorators.py
index 548f04a..dc57e13 100644
--- a/anta/decorators.py
+++ b/anta/decorators.py
@@ -2,40 +2,45 @@
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""decorators for tests."""
+
from __future__ import annotations
from functools import wraps
-from typing import TYPE_CHECKING, Any, Callable, Optional, TypeVar, cast
+from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast
from anta.models import AntaTest, logger
if TYPE_CHECKING:
from anta.result_manager.models import TestResult
-# TODO - should probably use mypy Awaitable in some places rather than this everywhere - @gmuloc
+# TODO: should probably use mypy Awaitable in some places rather than this everywhere - @gmuloc
F = TypeVar("F", bound=Callable[..., Any])
-def deprecated_test(new_tests: Optional[list[str]] = None) -> Callable[[F], F]:
- """
- Return a decorator to log a message of WARNING severity when a test is deprecated.
+def deprecated_test(new_tests: list[str] | None = None) -> Callable[[F], F]:
+ """Return a decorator to log a message of WARNING severity when a test is deprecated.
Args:
- new_tests (Optional[list[str]]): A list of new test classes that should replace the deprecated test.
+ ----
+ new_tests: A list of new test classes that should replace the deprecated test.
- Returns:
+ Returns
+ -------
Callable[[F], F]: A decorator that can be used to wrap test functions.
+
"""
def decorator(function: F) -> F:
- """
- Actual decorator that logs the message.
+ """Actual decorator that logs the message.
Args:
- function (F): The test function to be decorated.
+ ----
+ function: The test function to be decorated.
- Returns:
+ Returns
+ -------
F: The decorated function.
+
"""
@wraps(function)
@@ -43,9 +48,9 @@ def deprecated_test(new_tests: Optional[list[str]] = None) -> Callable[[F], F]:
anta_test = args[0]
if new_tests:
new_test_names = ", ".join(new_tests)
- logger.warning(f"{anta_test.name} test is deprecated. Consider using the following new tests: {new_test_names}.")
+ logger.warning("%s test is deprecated. Consider using the following new tests: %s.", anta_test.name, new_test_names)
else:
- logger.warning(f"{anta_test.name} test is deprecated.")
+ logger.warning("%s test is deprecated.", anta_test.name)
return await function(*args, **kwargs)
return cast(F, wrapper)
@@ -54,34 +59,37 @@ def deprecated_test(new_tests: Optional[list[str]] = None) -> Callable[[F], F]:
def skip_on_platforms(platforms: list[str]) -> Callable[[F], F]:
- """
- Return a decorator to skip a test based on the device's hardware model.
+ """Return a decorator to skip a test based on the device's hardware model.
This decorator factory generates a decorator that will check the hardware model of the device
the test is run on. If the model is in the list of platforms specified, the test will be skipped.
Args:
- platforms (list[str]): List of hardware models on which the test should be skipped.
+ ----
+ platforms: List of hardware models on which the test should be skipped.
- Returns:
+ Returns
+ -------
Callable[[F], F]: A decorator that can be used to wrap test functions.
+
"""
def decorator(function: F) -> F:
- """
- Actual decorator that either runs the test or skips it based on the device's hardware model.
+ """Actual decorator that either runs the test or skips it based on the device's hardware model.
Args:
- function (F): The test function to be decorated.
+ ----
+ function: The test function to be decorated.
- Returns:
+ Returns
+ -------
F: The decorated function.
+
"""
@wraps(function)
async def wrapper(*args: Any, **kwargs: Any) -> TestResult:
- """
- Check the device's hardware model and conditionally run or skip the test.
+ """Check the device's hardware model and conditionally run or skip the test.
This wrapper inspects the hardware model of the device the test is run on.
If the model is in the list of specified platforms, the test is either skipped.