Skip to content

Allow on_setattr=setters.NO_OP on frozen classes#1515

Open
bysiber wants to merge 1 commit intopython-attrs:mainfrom
bysiber:fix/frozen-no-op-on-setattr
Open

Allow on_setattr=setters.NO_OP on frozen classes#1515
bysiber wants to merge 1 commit intopython-attrs:mainfrom
bysiber:fix/frozen-no-op-on-setattr

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 20, 2026

Summary

Using on_setattr=setters.NO_OP on individual fields of a frozen class raises ValueError: Frozen classes can't use on_setattr, even though NO_OP explicitly means "do nothing for this attribute's setattr hook."

@attr.s(frozen=True)
class Frozen:
    x = attr.ib(on_setattr=setters.NO_OP)
# ValueError: Frozen classes can't use on_setattr.

This happens because the check if a.on_setattr is not None treats NO_OP the same as any real hook — NO_OP is an object() sentinel, not None, so it enters the frozen rejection branch.

The fix skips the rejection when on_setattr is specifically NO_OP, since it's semantically equivalent to having no hook at all. Real hooks like setters.validate are still correctly rejected on frozen classes.

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