Skip to content

record_metadata

yohou.testing.metadata_routing.record_metadata(obj, record_default=True, **params)

Store passed metadata to a method of obj.

If record_default is False, kwargs whose values are "default" are skipped. This is so that checks on keyword arguments whose default was not changed are skipped.

Parameters

Name Type Description Default
obj object

Object to record metadata on (usually has a registry attribute)

required
record_default bool

Whether to record kwargs with "default" values

True
**params dict

Metadata to record

{}

Source Code

Show/Hide source
def record_metadata(obj, record_default: bool = True, **params) -> None:
    """Store passed metadata to a method of obj.

    If record_default is False, kwargs whose values are "default" are skipped.
    This is so that checks on keyword arguments whose default was not changed
    are skipped.

    Parameters
    ----------
    obj : object
        Object to record metadata on (usually has a registry attribute)
    record_default : bool, default=True
        Whether to record kwargs with "default" values
    **params : dict
        Metadata to record

    """
    stack = inspect.stack()
    callee = stack[1].function
    caller = stack[2].function
    if not hasattr(obj, "_records"):
        obj._records = defaultdict(lambda: defaultdict(list))
    if not record_default:
        params = {key: val for key, val in params.items() if not isinstance(val, str) or (val != "default")}
    obj._records[callee][caller].append(params)