Skip to content

Commit

Permalink
doc: Doc nice deprecated tests (#945)
Browse files Browse the repository at this point in the history
---------
Co-authored-by: vitthalmagadum <vitthal.magadum-ext@arista.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
gmuloc authored Dec 10, 2024
1 parent 0efdd1c commit 1e23172
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
7 changes: 6 additions & 1 deletion anta/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any:
return decorator


def deprecated_test_class(new_tests: list[str] | None = None) -> Callable[[type[AntaTest]], type[AntaTest]]:
def deprecated_test_class(new_tests: list[str] | None = None, removal_in_version: str | None = None) -> Callable[[type[AntaTest]], type[AntaTest]]:
"""Return a decorator to log a message of WARNING severity when a test is deprecated.
Parameters
----------
new_tests
A list of new test classes that should replace the deprecated test.
removal_in_version
A string indicating the version in which the test will be removed.
Returns
-------
Expand Down Expand Up @@ -102,6 +104,9 @@ def new_init(*args: Any, **kwargs: Any) -> None:
logger.warning("%s test is deprecated.", cls.name)
orig_init(*args, **kwargs)

if removal_in_version is not None:
cls.__removal_in_version = removal_in_version

# NOTE: we are ignoring mypy warning as we want to assign to a method here
cls.__init__ = new_init # type: ignore[method-assign]
return cls
Expand Down
2 changes: 2 additions & 0 deletions anta/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ def test(self) -> None:
# Optional class attributes
name: ClassVar[str]
description: ClassVar[str]
__removal_in_version: ClassVar[str]
"""Internal class variable set by the `deprecated_test_class` decorator."""

# Mandatory class attributes
# TODO: find a way to tell mypy these are mandatory for child classes
Expand Down
6 changes: 3 additions & 3 deletions anta/tests/stun.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,13 @@ def test(self) -> None:
self.result.is_failure(f"{client_input} - Incorrect public-facing port - Expected: {input_public_port} Actual: {actual_public_port}")


@deprecated_test_class(new_tests=["VerifyStunClientTranslation"])
@deprecated_test_class(new_tests=["VerifyStunClientTranslation"], removal_in_version="v2.0.0")
class VerifyStunClient(VerifyStunClientTranslation):
"""(Deprecated) Verifies the translation for a source address on a STUN client.
Alias for the VerifyStunClientTranslation test to maintain backward compatibility.
When initialized, it will emit a deprecation warning and call the VerifyStunClientTranslation test.
TODO: Remove this class in ANTA v2.0.0.
Examples
--------
```yaml
Expand All @@ -113,6 +111,8 @@ class VerifyStunClient(VerifyStunClientTranslation):
```
"""

# TODO: Remove this class in ANTA v2.0.0.

# required to redefine name an description to overwrite parent class.
name = "VerifyStunClient"
description = "(Deprecated) Verifies the translation for a source address on a STUN client."
Expand Down
18 changes: 18 additions & 0 deletions docs/templates/python/material/class.html.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,21 @@
{{ super() }}
{% endif %}
{% endblock source %}

{# overwrite block base to render some stuff on deprecation for anta_test #}
{% block bases %}
{{ super() }}

{% for dec in class.decorators %}
{% if dec.value.function.name == "deprecated_test_class" %}
<img alt="Static Badge" src="https://img.shields.io/badge/DEPRECATED-yellow?style=flat&logoSize=auto">
{% for arg in dec.value.arguments | selectattr("name", "equalto", "removal_in_version") | list %}
<img alt="Static Badge" src="https://img.shields.io/badge/REMOVAL-{{ arg.value[1:-1] }}-grey?style=flat&logoSize=auto&labelColor=red">
{% endfor %}
<br/>
{% for arg in dec.value.arguments | selectattr("name", "equalto", "new_tests") | list %}
<strong>Replaced with:</strong> {{ arg.value.elements | map("replace", "'", "<code>", 1) | map("replace", "'", "</code>", 1) | join(", ") | safe }}
{% endfor %}
{% endif %}
{% endfor %}
{% endblock bases %}

0 comments on commit 1e23172

Please sign in to comment.