diff --git a/antarest/study/storage/variantstudy/business/utils.py b/antarest/study/storage/variantstudy/business/utils.py index 6f04601ec5..933c72bed7 100644 --- a/antarest/study/storage/variantstudy/business/utils.py +++ b/antarest/study/storage/variantstudy/business/utils.py @@ -52,10 +52,13 @@ def get_or_create_section(json_ini: JSON, section: str) -> JSON: def remove_none_args(command_dto: CommandDTO) -> CommandDTO: - if isinstance(command_dto.args, list): - command_dto.args = [{k: v for k, v in args.items() if v is not None} for args in command_dto.args] + args = command_dto.args + if isinstance(args, list): + command_dto.args = [{k: v for k, v in args.items() if v is not None} for args in args] + elif isinstance(args, dict): + command_dto.args = {k: v for k, v in args.items() if v is not None} else: - command_dto.args = {k: v for k, v in command_dto.args.items() if v is not None} + raise TypeError(f"Invalid type for args: {type(args)}") return command_dto diff --git a/antarest/study/storage/variantstudy/model/model.py b/antarest/study/storage/variantstudy/model/model.py index 4b2e15e2c1..cd478742b4 100644 --- a/antarest/study/storage/variantstudy/model/model.py +++ b/antarest/study/storage/variantstudy/model/model.py @@ -1,4 +1,4 @@ -from typing import List, Optional, Tuple, Union +import typing as t from pydantic import BaseModel @@ -7,19 +7,46 @@ class GenerationResultInfoDTO(BaseModel): + """ + Result information of a snapshot generation process. + + Attributes: + success: A boolean indicating whether the generation process was successful. + details: A list of tuples containing detailed information about the generation process. + """ + success: bool - details: List[Tuple[str, bool, str]] + details: t.MutableSequence[t.Tuple[str, bool, str]] class CommandDTO(BaseModel): - id: Optional[str] + """ + This class represents a command. + + Attributes: + id: The unique identifier of the command. + action: The action to be performed by the command. + args: The arguments for the command action. + version: The version of the command. + """ + + id: t.Optional[str] action: str - # if args is a list, this mean the command will be mapped to the list of args - args: Union[List[JSON], JSON] + args: t.Union[t.MutableSequence[JSON], JSON] version: int = 1 class CommandResultDTO(BaseModel): + """ + This class represents the result of a command. + + Attributes: + study_id: The unique identifier of the study. + id: The unique identifier of the command. + success: A boolean indicating whether the command was successful. + message: A message detailing the result of the command. + """ + study_id: str id: str success: bool @@ -27,7 +54,16 @@ class CommandResultDTO(BaseModel): class VariantTreeDTO: - # Don't use BaseModel as this could trigger Recursion exceptions, since we're using Pydantic with a version prior to v2. - def __init__(self, node: StudyMetadataDTO, children: List["VariantTreeDTO"]) -> None: + """ + This class represents a variant tree structure. + + Attributes: + node: The metadata of the study (ID, name, version, etc.). + children: A list of variant children. + """ + + def __init__(self, node: StudyMetadataDTO, children: t.MutableSequence["VariantTreeDTO"]) -> None: + # We are intentionally not using Pydantic’s `BaseModel` here to prevent potential + # `RecursionError` exceptions that can occur with Pydantic versions before v2. self.node = node - self.children = children + self.children = children or []