8000 bump: Pydantic 2 by gmuloc · Pull Request #250 · aristanetworks/anta · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

bump: Pydantic 2 #250

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 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


< 8000 strong class="js-conversation-menu-button">Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ repos:
args:
- --config-file=pyproject.toml
additional_dependencies:
- pydantic~=1.10
- pydantic~=2.0
- types-PyYAML
- types-paramiko
- types-requests
Expand Down
4 changes: 2 additions & 2 deletions anta/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ async def wrapper(*args: Any, **kwargs: Dict[str, Any]) -> TestResult:

command_output = cast(Dict[str, Any], command.output)

if "vrfs" not in command_output:
if "vrfs" not in command_output: # pylint: disable=unsupported-membership-test
anta_test.result.is_skipped(f"no BGP configuration for {family} on this device")
return anta_test.result
if len(bgp_vrfs := command_output["vrfs"]) == 0 or len(bgp_vrfs["default"]["peers"]) == 0:
if len(bgp_vrfs := command_output["vrfs"]) == 0 or len(bgp_vrfs["default"]["peers"]) == 0: # pylint: disable=unsubscriptable-object
# No VRF
anta_test.result.is_skipped(f"no {family} peer on this device")
return anta_test.result
Expand Down
18 changes: 9 additions & 9 deletions anta/inventory/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class AntaInventoryHost(BaseModel):
tags (List[str]): List of attached tags read from inventory file.
"""

name: Optional[str]
host: Union[constr(regex=RFC_1123_REGEX), IPvAnyAddress] # type: ignore
port: Optional[conint(gt=1, lt=65535)] # type: ignore
tags: Optional[List[str]]
name: Optional[str] = None
host: Union[constr(pattern=RFC_1123_REGEX), IPvAnyAddress] # type: ignore
port: Optional[conint(gt=1, lt=65535)] = None # type: ignore
tags: Optional[List[str]] = None


class AntaInventoryNetwork(BaseModel):
Expand All @@ -41,7 +41,7 @@ class AntaInventoryNetwork(BaseModel):
"""

network: IPvAnyNetwork
tags: Optional[List[str]]
tags: Optional[List[str]] = None


class AntaInventoryRange(BaseModel):
Expand All @@ -56,7 +56,7 @@ class AntaInventoryRange(BaseModel):

start: IPvAnyAddress
end: IPvAnyAddress
tags: Optional[List[str]]
tags: Optional[List[str]] = None


class AntaInventoryInput(BaseModel):
Expand All @@ -69,6 +69,6 @@ class AntaInventoryInput(BaseModel):
range (List[AntaInventoryRange],Optional): List of AntaInventoryRange objects for ranges.
"""

networks: Optional[List[AntaInventoryNetwork]]
hosts: Optional[List[AntaInventoryHost]]
ranges: Optional[List[AntaInventoryRange]]
networks: Optional[List[AntaInventoryNetwork]] = None
hosts: Optional[List[AntaInventoryHost]] = None
ranges: Optional[List[AntaInventoryRange]] = None
4 changes: 2 additions & 2 deletions anta/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class AntaTestCommand(BaseModel):
command: str
version: Union[int, Literal["latest"]] = "latest"
ofmt: str = "json"
output: Optional[Union[Dict[str, Any], str]]
output: Optional[Union[Dict[str, Any], str]] = None
template: Optional[AntaTestTemplate] = None
template_params: Optional[Dict[str, str]]
template_params: Optional[Dict[str, Any]] = None

@validator("template_params")
def prevent_none_when_template_is_set(cls: Type[AntaTestTemplate], value: Optional[Dict[str, str]]) -> Optional[Dict[str, str]]: # type: ignore
Expand Down
18 changes: 8 additions & 10 deletions anta/result_manager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Iterator, List

from pydantic import BaseModel, validator
from pydantic import BaseModel, RootModel, validator

RESULT_OPTIONS = ["unset", "success", "failure", "error", "skipped"]

Expand Down Expand Up @@ -119,36 +119,34 @@ def __str__(self) -> str:
return f"Test {self.test} on device {self.name} has result {self.result}"


class ListResult(BaseModel):
class ListResult(RootModel[List[TestResult]]):
"""
List result for all tests on all devices.

Attributes:
__root__ (List[TestResult]): A list of TestResult objects.
"""

# pylint: disable= 8000 R0801

__root__: List[TestResult] = []
root: List[TestResult] = []

def extend(self, values: List[TestResult]) -> None:
"""Add support for extend method."""
self.__root__.extend(values)
self.root.extend(values)

def append(self, value: TestResult) -> None:
"""Add support for append method."""
self.__root__.append(value)
self.root.append(value)

def __iter__(self) -> Iterator[TestResult]: # type: ignore
"""Use custom iter method."""
# TODO - mypy is not happy because we overwrite BaseModel.__iter__
# return type and are breaking Liskov Substitution Principle.
return iter(self.__root__)
return iter(self.root)

def __getitem__(self, item: int) -> TestResult:
"""Use custom getitem method."""
return self.__root__[item]
return self.root[item]

def __len__(self) -> int:
"""Support for length of __root__"""
return len(self.__root__)
return len(self.root)
2 changes: 2 additions & 0 deletions anta/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ async def main(
for device, test in itertools.product(inventory.get_inventory(established_only=established_only, tags=tags).values(), tests):
test_params = {k: v for k, v in test[1].items() if k != TEST_TPL_PARAMS}
template_params = test[1].get(TEST_TPL_PARAMS)
# TODO - catch pydantic_core._pydantic_core.ValidationError here.
# This may happen during AntaTestinstantiation due to wrong data for AntaCommand/AntaTemplate
coros.append(test[0](device=device, template_params=template_params).test(eos_data=None, **test_params))

logger.info("Running ANTA tests...")
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies = [
"click-help-colors==0.9.1",
"cvprac>=1.2.0",
"netaddr>=0.8.0",
"pydantic~=1.10",
"pydantic~=2.0",
"PyYAML>=6.0",
"requests",
"rich>=12.5.1",
Expand Down Expand Up @@ -59,13 +59,13 @@ dev = [
"mypy~=1.4",
"mypy-extensions~=1.0",
"pre-commit>=2.20.0",
"pylint>=2.16.1",
"pylint>=2.17",
"pytest>=7.1.2",
"pytest-cov>=2.11.1",
"pytest-dependency",
"pytest-html>=3.1.1",
"pytest-metadata>=1.11.0",
"pylint-pydantic>=0.1.4",
"pylint-pydantic>=0.2.0",
"tox==4.0.11",
"types-PyYAML",
"types-paramiko",
Expand Down
0