Skip to content

model_to_dict

Functions:

Name Description
model_to_dict

Functions#

model_to_dict #

model_to_dict(
    model: BaseModel, by_alias: bool = True
) -> Dict[Any, Any]

Converts the model to a dict with unset optional properties excluded.

Performs a nested union on the dictionaries produced by setting the exclude_unset and exclude_none options to True for the model.dict() method. This allows for the preservation of explicit None values in the yaml, while still filtering out values that default to None.

Borrowed approximately from https://stackoverflow.com/a/65363852/15641512.

Parameters:

Name Type Description Default
model BaseModel

The pydantic model to dict-ify.

required

Returns:

Type Description
Dict[Any, Any]

Dict[Any, Any]: The model as a dictionary.


Source code in tsdat/utils/model_to_dict.py
def model_to_dict(model: BaseModel, by_alias: bool = True) -> Dict[Any, Any]:
    """---------------------------------------------------------------------------------
    Converts the model to a dict with unset optional properties excluded.

    Performs a nested union on the dictionaries produced by setting the `exclude_unset`
    and `exclude_none` options to True for the `model.dict()` method. This allows for
    the preservation of explicit `None` values in the yaml, while still filtering out
    values that default to `None`.

    Borrowed approximately from https://stackoverflow.com/a/65363852/15641512.


    Args:
        model (BaseModel): The pydantic model to dict-ify.

    Returns:
        Dict[Any, Any]: The model as a dictionary.

    ---------------------------------------------------------------------------------"""
    return _nested_union(
        model.dict(exclude_unset=True, by_alias=by_alias),
        model.dict(exclude_none=True, by_alias=by_alias),
    )