debian-forge/osbuild/mixins.py
Michael Vogt f5d6d11f1d osbuild: error when {Device,Mount} is modified after creation
This is a drive-by change after spending some quality time with the
mount code. The `id` field of `Mount` is calculated only once and
only when creating a `Mount`. This seems slightly dangerous as
any change to an attribute after creation will not update the
id. This means two options:
1. dynamically update the `id` on changes
2. forbid changes after the `id` is calculcated

I went with (2) but happy to discuss of course but it seems more
the spirit of the class.

It also does the same change for "devices.Device"
2024-01-19 02:54:26 +01:00

15 lines
406 B
Python

"""
Mixin helper classes
"""
class MixinImmutableID:
"""
Mixin to ensure that "self.id" attributes are immutable after id is set
"""
def __setattr__(self, name, val):
if hasattr(self, "id"):
class_name = self.__class__.__name__
raise ValueError(f"cannot set '{name}': {class_name} cannot be changed after creation")
super().__setattr__(name, val)