Skip to content

yggdrasil.dataclasses.dataclass

dataclass

Dataclass helpers that integrate with Arrow schemas and safe casting.

get_from_dict

get_from_dict(
    obj: Mapping[str, Any], keys: Sequence[str], prefix: Optional[str]
) -> Any

Best-effort field lookup with optional prefix support.

Lookup order for each key

1) obj[key] 2) obj[prefix + key]

Returns:

Type Description
Any
  • first non-MISSING value found
Any
  • MISSING if nothing matched

default_value

default_value(f: Field[Any], with_factory: bool = True) -> Any

Return the effective default value for a dataclass field.

Returns:

Type Description
Any
  • f.default when present
Any
  • f.default_factory() when present
Any
  • MISSING otherwise

serialize_dataclass_state

serialize_dataclass_state(obj: Any) -> dict[str, Any]

Serialize constructor state for a dataclass instance.

Rules
  • only init=True fields are considered
  • private fields (name starts with "_") are skipped
  • None values are skipped
  • values equal to their effective default are skipped
  • output is a raw payload dict with no version envelope

restore_dataclass_state

restore_dataclass_state(obj: Any, state: Any) -> None

Restore dataclass state from a raw payload dict.

Rules
  • None is treated as {}
  • unknown keys are ignored
  • missing init=True fields are filled from effective defaults
  • missing required init=True fields raise TypeError
  • non-init fields are reset to their effective defaults when available

Raises:

Type Description
TypeError

If state is not a dict or a required field is missing.