Skip to content

check_compatibility

yohou_mlflow.check_compatibility(model_uri, extra_trusted_types=None, *, extra_trusted_prefixes=None, extra_version_rules=None, dst_path=None)

Report whether a saved forecaster would load here, without loading it.

Reads only the MLmodel file and the schema stored in the skops file. No object from the saved forecaster is constructed, so this is safe to run on an untrusted model, for example in a pre-deploy check.

Parameters

Name Type Description Default
model_uri str

Any MLflow model URI.

required
extra_trusted_types iterable of str or None

Exact type names the eventual load will trust in addition to the policy.

None
extra_trusted_prefixes iterable of str or None

Module prefixes the eventual load will trust, each ending in ".".

None
extra_version_rules mapping of str to str or None

Version rules the eventual load will apply beyond the built-in ones.

None
dst_path str or None

Local directory to download the model into.

None

Returns

Type Description
CompatibilityReport

loadable and one problem per reason a strict load would be refused.

Raises

Type Description
ValueError

If a trusted prefix is not a module path ending in ".", or a version rule is unknown or names a built-in package.

See Also

load_model : Load the forecaster once the report is clean.

Source Code

Source code in src/yohou_mlflow/_persistence.py
def check_compatibility(
    model_uri: str,
    extra_trusted_types: Iterable[str] | None = None,
    *,
    extra_trusted_prefixes: Iterable[str] | None = None,
    extra_version_rules: Mapping[str, str] | None = None,
    dst_path: str | None = None,
) -> CompatibilityReport:
    """Report whether a saved forecaster would load here, without loading it.

    Reads only the ``MLmodel`` file and the schema stored in the skops file. No object
    from the saved forecaster is constructed, so this is safe to run on an untrusted
    model, for example in a pre-deploy check.

    Parameters
    ----------
    model_uri : str
        Any MLflow model URI.
    extra_trusted_types : iterable of str or None, default=None
        Exact type names the eventual load will trust in addition to the policy.
    extra_trusted_prefixes : iterable of str or None, default=None
        Module prefixes the eventual load will trust, each ending in ``"."``.
    extra_version_rules : mapping of str to str or None, default=None
        Version rules the eventual load will apply beyond the built-in ones.
    dst_path : str or None, default=None
        Local directory to download the model into.

    Returns
    -------
    CompatibilityReport
        ``loadable`` and one problem per reason a strict load would be refused.

    Raises
    ------
    ValueError
        If a trusted prefix is not a module path ending in ``"."``, or a version rule is unknown or
        names a built-in package.

    See Also
    --------
    load_model : Load the forecaster once the report is clean.
    """
    validate_prefixes(extra_trusted_prefixes)
    validate_version_rules(extra_version_rules)
    local = download_artifacts(artifact_uri=model_uri, dst_path=dst_path)
    conf = _get_flavor_configuration(local, FLAVOR_NAME)
    try:
        format_version = _check_format_version(conf)
    except FormatVersionError as exc:
        # Nothing else in a format this release cannot read can be trusted to mean
        # what this release thinks it means.
        return CompatibilityReport(False, (str(exc),), str(conf.get("format_version", "")))

    problems = []
    mismatches = tuple(compare_versions(conf.get("versions", {}), extra_version_rules=extra_version_rules))
    problems.extend(f"A strict load would refuse the model, version mismatch: {m}" for m in mismatches)
    types = skops.io.get_untrusted_types(file=_component_path(local, conf, _FORECASTER_COMPONENT))
    outside = tuple(untrusted_outside_policy(types, extra_trusted_types, extra_trusted_prefixes))
    if outside:
        problems.append(
            f"A load would refuse the model, types outside the trust policy: {list(outside)}. "
            "Pass them in extra_trusted_types, or their package in extra_trusted_prefixes, if you trust them."
        )
    return CompatibilityReport(not problems, tuple(problems), format_version, mismatches, outside)