Skip to content

Pydantic feature types #204

@bstaber

Description

@bstaber

PR#198 introduces a pydantic model for CGNSNode but:

  • it's not used anywhere (we are not instantiating CGNSNodeobjects like node = CGNSNode(name="node", value=, ...)
  • and we are still type hinting functions with this type

What could be done:

  • properly use this object in functions that use in and out CGNSNode
  • at the same time, define pydantic models for other types (Scalar, Field, ...) (let's keep this for another issue)

Optional: we can also eventually define a constructor that accepts a list so that we can easily convert a pycgns "node" to a pydantic CGNSNode:

class CGNSNode(BaseModel):
    name: str
    value: Optional[Any] = None
    children: list["CGNSNode"]
    label: str

    @model_validator(mode="before")
    @classmethod
    def from_list(cls, data: Any) -> Any:
        # If the input is already a dict, let Pydantic handle it
        if isinstance(data, dict):
            return data
        # If the input is a list, convert it
        if isinstance(data, list) and len(data) == 4:
            name, value, children, label = data
            return {
                "name": name,
                "value": value,
                "children": children,
                "label": label,
            }
        return data

This gives access to a simple constructor: node = CGNSNode(base_node) where base_node would be a CGNS node in raw format (list) given by pyCGNS.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions