Skip to content

assert_request_is_empty

yohou.testing.metadata_routing.assert_request_is_empty(metadata_request, exclude=None)

Check if a metadata request dict is empty.

One can exclude a method or a list of methods from the check using the exclude parameter. If metadata_request is a MetadataRouter, then exclude can be of the form {"object" : [method, ...]}.

Parameters

Name Type Description Default
metadata_request MetadataRequest | MetadataRouter

The request object to check

required
exclude str | list[str] | dict

Methods or objects to exclude from the check

None

Raises

Type Description
AssertionError

If metadata request is not empty

Source Code

Show/Hide source
def assert_request_is_empty(metadata_request, exclude=None) -> None:
    """Check if a metadata request dict is empty.

    One can exclude a method or a list of methods from the check using the
    ``exclude`` parameter. If metadata_request is a MetadataRouter, then
    ``exclude`` can be of the form ``{"object" : [method, ...]}``.

    Parameters
    ----------
    metadata_request : MetadataRequest | MetadataRouter
        The request object to check
    exclude : str | list[str] | dict, optional
        Methods or objects to exclude from the check

    Raises
    ------
    AssertionError
        If metadata request is not empty

    """
    if isinstance(metadata_request, MetadataRouter):
        for name, route_mapping in metadata_request:
            _exclude = exclude[name] if exclude is not None and name in exclude else None
            assert_request_is_empty(route_mapping.router, exclude=_exclude)
        return

    exclude = [] if exclude is None else exclude
    # Yohou methods: fit, predict, observe, observe_predict, observe_transform, transform, score
    yohou_methods = [
        "fit",
        "predict",
        "observe",
        "observe_predict",
        "observe_transform",
        "transform",
        "inverse_transform",
        "score",
    ]
    for method in yohou_methods:
        if method in exclude:
            continue
        mmr = getattr(metadata_request, method, None)
        if mmr is None:
            continue
        props = [
            prop
            for prop, alias in mmr.requests.items()
            if (isinstance(alias, str) or alias is not None) and prop not in exclude
        ]
        assert not props, f"Method {method} has non-empty requests: {props}"