Skip to content

Commit a572c3a

Browse files
committed
Allow field(on_setattr=NO_OP) on frozen classes
1 parent af9c510 commit a572c3a

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

changelog.d/1515.change.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Frozen classes can set `on_setattr=attrs.setters.NO_OP` in addition to `None`.

src/attr/_make.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2022,7 +2022,7 @@ def _make_init_script(
20222022
attr_dict[a.name] = a
20232023

20242024
if a.on_setattr is not None:
2025-
if frozen is True:
2025+
if frozen is True and a.on_setattr is not setters.NO_OP:
20262026
msg = "Frozen classes can't use on_setattr."
20272027
raise ValueError(msg)
20282028

tests/test_setattr.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ class C:
221221

222222
assert "Frozen classes can't use on_setattr." == ei.value.args[0]
223223

224+
@pytest.mark.parametrize("nop", [None, setters.NO_OP])
225+
def test_frozen_on_setattr_nops(self, nop):
226+
"""
227+
on_setattr on frozen classes can be used for None and NO_OP.
228+
"""
229+
230+
@attr.s(frozen=True)
231+
class C:
232+
x = attr.ib(on_setattr=nop)
233+
224234
def test_setattr_reset_if_no_custom_setattr(self, slots):
225235
"""
226236
If a class with an active setattr is subclassed and no new setattr

0 commit comments

Comments
 (0)