Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: forbid delete attr from frozen class #118

Merged
merged 2 commits into from Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.rst
Expand Up @@ -24,8 +24,8 @@ Deprecations:
Changes:
^^^^^^^^

*none*

- Raise ``FrozenInstanceError`` when trying to delete an attribute from
a frozen class. `#118 <https://github.com/hynek/attrs/pull/118>`__

----

Expand Down
8 changes: 8 additions & 0 deletions src/attr/_make.py
Expand Up @@ -201,6 +201,13 @@ def _frozen_setattrs(self, name, value):
raise FrozenInstanceError()


def _frozen_delattrs(self, name):
"""
Attached to frozen classes as __delattr__.
"""
raise FrozenInstanceError()


def attributes(maybe_cls=None, these=None, repr_ns=None,
repr=True, cmp=True, hash=True, init=True,
slots=False, frozen=False, str=False):
Expand Down Expand Up @@ -291,6 +298,7 @@ def wrap(cls):
cls = _add_init(cls, frozen)
if frozen is True:
cls.__setattr__ = _frozen_setattrs
cls.__delattr__ = _frozen_delattrs
if slots is True:
# slots and frozen require __getstate__/__setstate__ to work
cls = _add_pickle(cls)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_dark_magic.py
Expand Up @@ -182,6 +182,9 @@ def test_frozen_instance(self, frozen_class):
with pytest.raises(FrozenInstanceError) as e:
frozen.x = 2

with pytest.raises(FrozenInstanceError) as e:
del frozen.x

assert e.value.args[0] == "can't set attribute"
assert 1 == frozen.x

Expand Down