Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions semimutable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ def __set__(self, instance: object, value: T) -> None:

setattr(instance, self._private_name, value)

def __delete__(self, instance: object) -> None:
try:
delattr(instance, self._private_name)
except AttributeError:
pass
public_name = self._private_name[len(FROZEN_PREFIX) :]
try:
instance.__dict__.pop(public_name, None)
except AttributeError:
pass


error = RuntimeError(
"This field is created via field(frozen=True) but the @semimutable.dataclass decorator is not used on the dataclass. "
Expand Down
11 changes: 11 additions & 0 deletions tests/test_semimutable_dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ class Sm:
sm.y = 42


def test_frozen_field_can_be_deleted():
@dataclass(slots=True)
class Sm:
x: int = field(frozen=True)

sm = Sm(x=1)
del sm.x
with pytest.raises(AttributeError):
sm.x


def test_plain_dataclass_is_refused():
with pytest.raises(RuntimeError):

Expand Down