mlflow/mlflow · error · RuntimeError

Cannot revert the attribute named '%s' since the setting 'st

Error message

Cannot revert the attribute named '%s' since the setting 'store_hit' was not set to True when applying the patch.

What it means

gorilla.revert() cannot restore the original attribute because the patch that overwrote it was applied with store_hit=False (or default settings), so no _original_<name> backup exists in the destination's __dict__. For in-place patches (patch.name was already in destination.__dict__ at apply time), reverting requires that stored original. MLflow raises RuntimeError rather than silently losing the pre-patch implementation.

Source

Thrown at mlflow/utils/gorilla.py:359

    Notice:
    This method is taken from
    https://github.com/christophercrouzet/gorilla/blob/v0.4.0/gorilla.py#L318-L351
    with modifictions for autologging disablement purposes.
    """
    # If an curr_active_patch has not been set on destination class for the current patch,
    # then the patch has not been applied and we do not need to revert anything.
    curr_active_patch = _ACTIVE_PATCH % (patch.name,)
    if curr_active_patch not in patch.destination.__dict__:
        # already reverted.
        return

    original_name = _ORIGINAL_NAME % (patch.name,)

    if patch.is_inplace_patch:
        # check whether original_name is in destination. We cannot use hasattr because it will
        # try to get attribute from parent classes if attribute not found in destination class.
        if original_name not in patch.destination.__dict__:
            raise RuntimeError(
                "Cannot revert the attribute named '%s' since the setting "  # noqa: UP031
                "'store_hit' was not set to True when applying the patch."
                % (patch.destination.__name__,)
            )
        # restore original method
        # during reverting patch, we need restore the raw attribute to the patch point
        # so get original attribute bypassing descriptor protocal
        original = object.__getattribute__(patch.destination, original_name)
        setattr(patch.destination, patch.name, original)
    else:
        # delete patched method
        delattr(patch.destination, patch.name)

    if original_name in patch.destination.__dict__:
        delattr(patch.destination, original_name)
    delattr(patch.destination, curr_active_patch)

View on GitHub (pinned to 6a27f2decc)

Solutions

  1. Re-apply the patch with gorilla.Settings(allow_hit=True, store_hit=True), then revert it
  2. Manually restore the original implementation: set patch.destination.<name> back to the original function you saved before patching
  3. If the original is unrecoverable, delete the patched attribute and re-set it from the parent class or a fresh class definition
  4. Avoid mixing: standardize on store_hit=True wherever allow_hit=True is used

Example fix

// before
gorilla.apply(gorilla.Patch(Cls, 'fit', wrapper, gorilla.Settings(allow_hit=True)))
...
gorilla.revert(patch)  # RuntimeError: store_hit was not True
// after
gorilla.apply(gorilla.Patch(Cls, 'fit', wrapper, gorilla.Settings(allow_hit=True, store_hit=True)))
...
gorilla.revert(patch)
Defensive patterns

Strategy: try-catch

Validate before calling

orig = '_original_' + patch.name
if patch.is_inplace_patch and orig not in patch.destination.__dict__:
    raise RuntimeError('Cannot revert: re-apply patch with store_hit=True first')

Type guard

def is_revertible(patch) -> bool:
    return (not patch.is_inplace_patch) or ('_original_%s' % patch.name) in patch.destination.__dict__

Try / catch

try:
    gorilla.revert(patch)
except RuntimeError as e:
    if 'store_hit' in str(e):
        logger.warning('Original not stored; manually restoring %s', patch.name)
        setattr(patch.destination, patch.name, original_impl)
    else:
        raise

Prevention

When it happens

Trigger: Calling gorilla.revert(patch) (directly or via revert_patches / patched_fit teardown) where patch.is_inplace_patch is True and the key '_original_<name>' is not in patch.destination.__dict__, i.e. the patch was applied without Settings(store_hit=True).

Common situations: Autologging was enabled with allow_hit=True but store_hit left False, then mlflow.autolog(disable=True) or revert_patches() runs; test teardown trying to undo a patch applied manually in setup; mixed patch versions where one process patched with store_hit and another tries to revert.

Related errors


AI-assisted analysis of mlflow/mlflow@6a27f2decc (2026-08-29). Data as JSON: /api/errors/6aaab029f203c95a. Report an issue: GitHub.