Skip to content

declearn.utils.access_registered

Access a registered type by its name.

Parameters:

Name Type Description Default
name str

Name under which the type is registered.

required
group Optional[str]

Name of the TypesRegistry under which the type is stored. If None, look for the name in each and every registry and return the first-found match or raise a KeyError.

None

Returns:

Name Type Description
cls type

Type retrieved from a types registry.

Raises:

Type Description
KeyError

If no registered type matching the input parameters is found.

Source code in declearn/utils/_register.py
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
def access_registered(
    name: str,
    group: Optional[str] = None,
) -> Type:
    """Access a registered type by its name.

    Parameters
    ----------
    name: str
        Name under which the type is registered.
    group: str or None, default=None
        Name of the TypesRegistry under which the type is stored.
        If None, look for the name in each and every registry and
        return the first-found match or raise a KeyError.

    Returns
    -------
    cls: type
        Type retrieved from a types registry.

    Raises
    ------
    KeyError
        If no registered type matching the input parameters is found.
    """
    # If group is unspecified, look the name up in each and every registry.
    if group is None:
        for reg in REGISTRIES.values():
            try:
                return reg.access(name)
            except KeyError:
                continue
        raise KeyError(f"No '{name}' entry under any types registry.")
    # Otherwise, look up the registry, then access the target type from it.
    if group not in REGISTRIES:
        raise KeyError(f"Type registry '{group}' does not exist.")
    return REGISTRIES[group].access(name)