Skip to content

declearn.utils.access_registration_info

Access a registered type's storage name and belonging group.

Parameters:

Name Type Description Default
cls Type

Registered type, the storage name of which to retrive.

required
group Optional[str]

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

None

Returns:

Name Type Description
name str

Name under which the type is registered.

group str

Name of the TypesRegistry in which the type is registered.

Raises:

Type Description
KeyError

If the provided information does not match a registered type.

Source code in declearn/utils/_register.py
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
def access_registration_info(
    cls: Type,
    group: Optional[str] = None,
) -> Tuple[str, str]:
    """Access a registered type's storage name and belonging group.

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

    Returns
    -------
    name: str
        Name under which the type is registered.
    group: str
        Name of the TypesRegistry in which the type is registered.

    Raises
    ------
    KeyError
        If the provided information does not match a registered type.
    """
    # If group is unspecified, look the type up in each and every registry.
    if group is None:
        for grp, reg in REGISTRIES.items():
            try:
                return reg.get_name(cls), grp
            except KeyError:
                continue
        raise KeyError(
            f"Type '{cls.__name__}' not found under any types registry."
        )
    # Otherwise, look up the registry, then get the target name from it.
    if group not in REGISTRIES:
        raise KeyError(f"Type registry '{group}' does not exist.")
    return REGISTRIES[group].get_name(cls), group