GitHub

ExtendTo

API reference for ExtendTo.

class ExtendTo[TD: Mapping[str, object], A: Mapping[str, object]]

extend_to(typed_dict, derive): apply with .pipe to a struct.

Functions

__init__

__init__(typed_dict, derive, fields) -> None
ParameterTypeDescription
typed_dicttype[TD]
deriveMapping[str, Callable[[A], Option[object]]]
fieldsMapping[str, Codec[object, object, Any, Any]]
def __init__(
    self,
    typed_dict: type[TD],
    derive: Mapping[str, Callable[[A], Option[object]]],
    fields: Mapping[str, Codec[object, object, Any, Any]],
) -> None:
    self._typed_dict = typed_dict
    self._derive = dict(derive)
    self._fields = dict(fields)

__call__

__call__[RD, RE](schema) -> DecodeToSchema[TD, Mapping[str, object], RD, RE]
ParameterTypeDescription
schemaStructSchema[A, RD, RE]

Returns DecodeToSchema[TD, Mapping[str, object], RD, RE]

def __call__[RD, RE](
    self, schema: StructSchema[A, RD, RE], /
) -> DecodeToSchema[TD, Mapping[str, object], RD, RE]:
    kept = {name: to_type(field) for name, field in schema.fields.items()}
    target = Struct(self._typed_dict, **kept, **self._fields)
    new = [key for key in target.fields if key not in kept]
    if set(new) != set(self._derive):
        msg = (
            f"extend_to derives {stringify(list(self._derive))}, new keys are {stringify(new)}"
        )
        raise ValueError(msg)
    transformation = transform(decode=self._decode, encode=self._encode)
    node = ast.decode_to(schema.__codec__().ast, target.__codec__().ast, transformation)
    return DecodeToSchema(node, schema, target)