Skip to content

mistral_common.deprecation

deprecated_import(old_path, new_module, name, version)

Warn once and lazily import a symbol that moved to a new module.

Parameters:

Name Type Description Default
old_path str

The old module path (e.g. "mistral_common.audio").

required
new_module str

The new module path (e.g. "mistral_common.tokens.tokenizers.audio").

required
name str

The symbol name (e.g. "Audio").

required
version str

The version in which the symbol will be removed.

required

Returns:

Type Description
object

The imported symbol from the new module.

Source code in src/mistral_common/deprecation.py
def deprecated_import(old_path: str, new_module: str, name: str, version: str) -> object:
    r"""Warn once and lazily import a symbol that moved to a new module.

    Args:
        old_path: The old module path (e.g. `"mistral_common.audio"`).
        new_module: The new module path (e.g. `"mistral_common.tokens.tokenizers.audio"`).
        name: The symbol name (e.g. `"Audio"`).
        version: The version in which the symbol will be removed.

    Returns:
        The imported symbol from the new module.
    """
    key = f"import:{old_path}.{name}"
    if key not in _warned_keys:
        _warned_keys.add(key)
        warnings.warn(
            f"Importing {name} from {old_path} is deprecated. "
            f"Use {new_module}.{name} instead. "
            f"Will be removed in {version}.",
            DeprecationWarning,
            stacklevel=3,
        )
    mod = importlib.import_module(new_module)
    return getattr(mod, name)

warn_once(key, message, category, stacklevel)

Emit a warning only on the first call for a given key.

Parameters:

Name Type Description Default
key str

Unique identifier for this warning.

required
message str

The warning message.

required
category type[Warning]

The warning category class.

required
stacklevel int

Stack level for the warning.

required
Source code in src/mistral_common/deprecation.py
def warn_once(key: str, message: str, category: type[Warning], stacklevel: int) -> None:
    r"""Emit a warning only on the first call for a given key.

    Args:
        key: Unique identifier for this warning.
        message: The warning message.
        category: The warning category class.
        stacklevel: Stack level for the warning.
    """
    if key not in _warned_keys:
        _warned_keys.add(key)
        warnings.warn(message, category, stacklevel=stacklevel)