dataclass_from_attributes

dataclass_from_attributes#

penzai.core.dataclass_util.dataclass_from_attributes(cls: type[T], **field_values) T[source]#

Directly instantiates a dataclass given all of its fields.

Dataclasses can override __init__ to have arbitrary custom behavior, but this may make it difficult to construct new instances of dataclasses with particular field values. This function makes it possible to directly instantiate an instance of a dataclass with given attributes.

Callers of this method are responsible for maintaining any invariants expected by the class. The intended use of this function is to restore a dataclass from field values extracted from another instance of that exact dataclass type.

Parameters:
  • cls – Class to instantiate.

  • **field_values – Values for each of the dataclass’s fields

Returns:

A new instance of the class.