Skip to content
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

fix(anta): Implement Pydantic class to replace RawCatalogInput type #906

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
38 changes: 33 additions & 5 deletions anta/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,36 @@

logger = logging.getLogger(__name__)

# { <module_name> : [ { <test_class_name>: <input_as_dict_or_None> }, ... ] }
RawCatalogInput = dict[str, list[dict[str, Optional[dict[str, Any]]]]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


class RawCatalogInputModuleOptionModel(RootModel[Union[dict[str, Any], None]]): # pylint: disable=R0903
"""Model capturing test option in catalog input."""

root: dict[str, Any] | None


class RawCatalogInputModuleModel(RootModel[dict[str, RawCatalogInputModuleOptionModel]]): # pylint: disable=R0903
"""Model capturing test module in catalog input."""

root: dict[str, RawCatalogInputModuleOptionModel]


class RawCatalogInputModel(RootModel[dict[str, list[RawCatalogInputModuleModel]]]): # pylint: disable=R0903
"""Model for tests catalog input defined by user.

Originally defined with:
RawCatalogInput = dict[str, list[dict[str, Optional[dict[str, Any]]]]]

Example:
raw_catalog_input = RawCatalogInputModel.parse_obj({
"module_name": [
{"test_name": {"key": "value"}},
{"another_test": None}
]
})
"""

root: dict[str, list[RawCatalogInputModuleModel]]


# [ ( <AntaTest class>, <input_as AntaTest.Input or dict or None > ), ... ]
ListAntaTestTuples = list[tuple[type[AntaTest], Optional[Union[AntaTest.Input, dict[str, Any]]]]]
Expand Down Expand Up @@ -358,10 +386,10 @@ def parse(filename: str | Path, file_format: Literal["yaml", "json"] = "yaml") -
return AntaCatalog.from_dict(data, filename=filename)

@staticmethod
def from_dict(data: RawCatalogInput, filename: str | Path | None = None) -> AntaCatalog:
def from_dict(data: RawCatalogInputModel, filename: str | Path | None = None) -> AntaCatalog:
"""Create an AntaCatalog instance from a dictionary data structure.

See RawCatalogInput type alias for details.
See RawCatalogInputModel type alias for details.
It is the data structure returned by `yaml.load()` function of a valid
YAML Test Catalog file.

Expand All @@ -387,7 +415,7 @@ def from_dict(data: RawCatalogInput, filename: str | Path | None = None) -> Anta
raise TypeError(msg)

try:
catalog_data = AntaCatalogFile(data) # type: ignore[arg-type]
catalog_data = AntaCatalogFile(data)
except ValidationError as e:
anta_log_exception(
e,
Expand Down
Loading