Skip to content

declearn.utils.register_type

Register a class in a registry, to ease its (de)serialization.

Note: this function may either be used to register an existing type through functional syntax, or placed as a decorator for class-defining code

Parameters:

Name Type Description Default
cls Optional[Type]

Class that is to be registered. If None, return a class decorator.

None
name Optional[str]

Name under which the type should be registered, and hence retrievable from. If None, use cls.__name__.

None
group Optional[str]

Name of the TypesRegistry to which the class should be added (created using create_types_registry). If None, use the first-found existing registry that has a compatible base type, or raise a TypeError.

None

Returns:

Name Type Description
cls type

The input cls; hence this function may be used as a decorator to register classes in the source code.

Source code in declearn/utils/_register.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
def register_type(
    cls: Optional[Type] = None,
    name: Optional[str] = None,
    group: Optional[str] = None,
) -> Type:
    """Register a class in a registry, to ease its (de)serialization.

    Note: this function may either be used to register an existing
          type through functional syntax, or placed as a decorator
          for class-defining code

    Parameters
    ----------
    cls: type or None, default=None
        Class that is to be registered.
        If None, return a class decorator.
    name: str or None, default=None
        Name under which the type should be registered, and
        hence retrievable from. If None, use `cls.__name__`.
    group: str or None, default=None
        Name of the TypesRegistry to which the class should
        be added (created using `create_types_registry`).
        If None, use the first-found existing registry that
        has a compatible base type, or raise a TypeError.

    Returns
    -------
    cls: type
        The input `cls`; hence this function may be used as
        a decorator to register classes in the source code.
    """
    # Case when the function is being used as a class decorator.
    if cls is None:
        decorator = functools.partial(register_type, name=name, group=group)
        return decorator  # type: ignore
    # Optionnally infer the registry to use. Otherwise, check existence.
    if group is None:
        for key, reg in REGISTRIES.items():
            if issubclass(cls, reg.base):
                group = key
                break
        else:
            raise TypeError("Could not infer registration group.")
    elif group not in REGISTRIES:
        raise KeyError(f"Type registry '{group}' does not exist.")
    # Register the type in the target registry.
    REGISTRIES[group].register(cls, name)
    # Return the input type (enabling use as a decorator).
    return cls