8000 doc: Doc nice deprecated tests by gmuloc · Pull Request #945 · aristanetworks/anta · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

doc: Doc nice deprecated tests #945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 %}
Loading
0