Skip to content

config_context

sklearn_wrap.config.config_context(*, trusted_modules=None)

Context manager to temporarily set sklearn-wrap configuration.

Configuration is restored to its previous state when the context exits, even if an exception is raised.

Parameters

Name Type Description Default
trusted_modules frozenset[str] or None

If not None, temporarily replace the trusted modules set.

None

Examples

>>> with config_context(trusted_modules=frozenset({"sklearn", "xgboost"})):
...     get_config()["trusted_modules"] == frozenset({"sklearn", "xgboost"})
True
>>> get_config()["trusted_modules"] == DEFAULT_TRUSTED_MODULES
True

See Also

set_config : Persistently update the configuration. get_config : Retrieve the current configuration.

Source Code

Show/Hide source
@contextlib.contextmanager
def config_context(*, trusted_modules: frozenset[str] | None = None) -> Generator[None, None, None]:
    """Context manager to temporarily set sklearn-wrap configuration.

    Configuration is restored to its previous state when the context exits,
    even if an exception is raised.

    Parameters
    ----------
    trusted_modules : frozenset[str] or None
        If not ``None``, temporarily replace the trusted modules set.

    Examples
    --------
    >>> with config_context(trusted_modules=frozenset({"sklearn", "xgboost"})):
    ...     get_config()["trusted_modules"] == frozenset({"sklearn", "xgboost"})
    True
    >>> get_config()["trusted_modules"] == DEFAULT_TRUSTED_MODULES
    True

    See Also
    --------
    set_config : Persistently update the configuration.
    get_config : Retrieve the current configuration.
    """
    old_config = getattr(_threadlocal, "config", {}).copy()
    try:
        set_config(trusted_modules=trusted_modules)
        yield
    finally:
        _threadlocal.config = old_config