From 6728e006006576166c854d99786f96454af15841 Mon Sep 17 00:00:00 2001 From: Pedro Crespo-Valero <32402063+pcrespov@users.noreply.github.com> Date: Tue, 29 Oct 2024 09:55:40 +0100 Subject: [PATCH] fixes pydantic models search --- .../src/pytest_simcore/pydantic_models.py | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/packages/pytest-simcore/src/pytest_simcore/pydantic_models.py b/packages/pytest-simcore/src/pytest_simcore/pydantic_models.py index 04d285a601eb..05e85361ebae 100644 --- a/packages/pytest-simcore/src/pytest_simcore/pydantic_models.py +++ b/packages/pytest-simcore/src/pytest_simcore/pydantic_models.py @@ -82,21 +82,20 @@ def _is_model_cls(obj) -> bool: for model_name, model_cls in inspect.getmembers(module, _is_model_cls): assert model_name # nosec if ( - (config_cls := model_cls.model_config) - and inspect.isclass(config_cls) - and is_strict_inner(model_cls, config_cls) - and (schema_extra := getattr(config_cls, "schema_extra", {})) - and isinstance(schema_extra, dict) + (model_config := model_cls.model_config) + and isinstance(model_config, dict) + and (json_schema_extra := model_config.get("json_schema_extra", {})) + and isinstance(json_schema_extra, dict) ): - if "example" in schema_extra: + if "example" in json_schema_extra: yield ModelExample( model_cls=model_cls, example_name="example", - example_data=schema_extra["example"], + example_data=json_schema_extra["example"], ) - elif "examples" in schema_extra: - for index, example in enumerate(schema_extra["examples"]): + elif "examples" in json_schema_extra: + for index, example in enumerate(json_schema_extra["examples"]): yield ModelExample( model_cls=model_cls, example_name=f"examples_{index}", @@ -120,10 +119,10 @@ def model_cls_examples(model_cls: type[BaseModel]) -> dict[str, dict[str, Any]]: "SEE https://pydantic-docs.helpmanual.io/usage/schema/#schema-customization" ) + json_schema_extra: dict = model_cls.model_config.get("json_schema_extra", {}) + # checks exampleS setup in schema_extra - examples_list = copy.deepcopy( - model_cls.model_config["json_schema_extra"].get("examples", []) - ) + examples_list = copy.deepcopy(json_schema_extra.get("examples", [])) assert isinstance(examples_list, list), ( "OpenAPI and json-schema differ regarding the format for exampleS." "The former is a dict and the latter an array. " @@ -132,15 +131,12 @@ def model_cls_examples(model_cls: type[BaseModel]) -> dict[str, dict[str, Any]]: "SEE https://swagger.io/docs/specification/adding-examples/" ) - # check example in schema_extra - example = copy.deepcopy(model_cls.model_config["json_schema_extra"].get("example")) - # collect all examples and creates fixture -> {example-name: example, ...} examples = { - f"{model_cls.__name__}.example[{index}]": example - for index, example in enumerate(examples_list) + f"{model_cls.__name__}.example[{index}]": example_ + for index, example_ in enumerate(examples_list) } - if example: + if example := copy.deepcopy(json_schema_extra.get("example")): examples[f"{model_cls.__name__}.example"] = example return examples