def asdict()

in chz/data_model.py [0:0]


def asdict(obj: object) -> dict[str, Any]:
    """Recursively convert a chz object to a dict.

    This works similarly to dataclasses.asdict. Note no computed properties will be included
    in the output.

    See also: beta_to_blueprint_values
    """

    def inner(x: Any):
        if hasattr(x, "__chz_fields__"):
            return {k: inner(getattr(x, k)) for k in x.__chz_fields__}
        if isinstance(x, dict):
            return {k: inner(v) for k, v in x.items()}
        if isinstance(x, list):
            return [inner(x) for x in x]
        if isinstance(x, tuple):
            return tuple(inner(x) for x in x)
        return copy.deepcopy(x)

    if not hasattr(obj, "__chz_fields__"):
        raise RuntimeError(f"{obj} is not a chz object")

    result = inner(obj)
    assert type(result) is dict
    return result