Skip to content

Fix validators.disabled() to save/restore state on nesting#1513

Open
bysiber wants to merge 1 commit intopython-attrs:mainfrom
bysiber:fix/disabled-context-manager-nesting
Open

Fix validators.disabled() to save/restore state on nesting#1513
bysiber wants to merge 1 commit intopython-attrs:mainfrom
bysiber:fix/disabled-context-manager-nesting

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 20, 2026

Summary

validators.disabled() unconditionally re-enables validators in its finally block instead of restoring the previous state. This breaks nested usage — the inner context manager prematurely re-enables validators when it exits.

with disabled():
    with disabled():
        pass
    # BUG: validators are now ENABLED even though we're still in the outer disabled()
    C(x="not_int")  # raises TypeError unexpectedly

The fix saves the current state with get_run_validators() before disabling, then restores it in finally. This is a one-line semantic change.

The same pattern of nesting breakage also applies when set_run_validators(False) was called before entering disabled() — the context manager would unexpectedly override that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant