Skip to content

declearn.utils.deserialize_object

Return an object from a ObjectConfig serialization or JSON file.

This function is the counterpart to declearn.utils.serialize_object.

Parameters:

Name Type Description Default
config Union[str, ObjectConfig, ObjectConfigDict]

Either an ObjectConfig object, the dict representation of one, or the path to a JSON file that stores an ObjectConfig dump.

required
custom Optional[Dict[str, Type[SupportsConfig]]]

Optional dict providing with a {name: type} mapping to enable deserializing user-defined types that have not been registered using declearn.utils.register_type.

None

Returns:

Name Type Description
obj object

An object instantiated from the provided configuration.

Raises:

Type Description
TypeError

If config is not or cannot be transformed into an ObjectConfig.

KeyError

If config specifies an object type that cannot be retrieved.

Source code in declearn/utils/_serialize.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def deserialize_object(
    config: Union[str, ObjectConfig, ObjectConfigDict],
    custom: Optional[Dict[str, Type[SupportsConfig]]] = None,
) -> SupportsConfig:
    """Return an object from a ObjectConfig serialization or JSON file.

    This function is the counterpart to `declearn.utils.serialize_object`.

    Parameters
    ----------
    config: ObjectConfig or str
        Either an ObjectConfig object, the dict representation of one,
        or the path to a JSON file that stores an ObjectConfig dump.
    custom: dict[str, object] or None, default=None
        Optional dict providing with a {name: type} mapping to enable
        deserializing user-defined types that have not been registered
        using `declearn.utils.register_type`.

    Returns
    -------
    obj: object
        An object instantiated from the provided configuration.

    Raises
    ------
    TypeError
        If `config` is not or cannot be transformed into an ObjectConfig.
    KeyError
        If `config` specifies an object type that cannot be retrieved.
    """
    if isinstance(config, str):
        config = ObjectConfig.from_json(config)
    elif isinstance(config, dict):
        try:
            config = ObjectConfig(**config)
        except TypeError as exc:
            raise TypeError(
                "deserialize_object received a 'config' dict that does not "
                "conform with the ObjectConfig specification."
            ) from exc
    elif not isinstance(config, ObjectConfig):
        raise TypeError(
            "Unproper type for argument 'config' of deserialize_object: "
            f"'{type(config)}'."
        )
    try:
        cls = access_registered(config.name, config.group)
    except KeyError as exception:
        if custom is None:
            raise exception
        if config.name not in custom:
            msg = f"No custom type provided for name '{config.name}'"
            raise KeyError(msg) from exception
        cls = custom[config.name]
    return cls.from_config(config.config)