|
| 1 | +""" |
| 2 | +The mutability API. |
| 3 | +
|
| 4 | +This provides tools to mark objects as mutable or immutable, and to guard against mutations. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from typing import Any, TYPE_CHECKING |
| 10 | + |
| 11 | +from betty.typing import internal |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from collections.abc import Iterable |
| 15 | + |
| 16 | + |
| 17 | +class MutabilityError(Exception): |
| 18 | + """ |
| 19 | + A generic mutability API error. |
| 20 | + """ |
| 21 | + |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +@internal |
| 26 | +class MutableError(MutabilityError, RuntimeError): |
| 27 | + """ |
| 28 | + An error raised because something was unexpectedly mutable. |
| 29 | + """ |
| 30 | + |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +@internal |
| 35 | +class ImmutableError(MutabilityError, RuntimeError): |
| 36 | + """ |
| 37 | + An error raised because something was unexpectedly immutable. |
| 38 | + """ |
| 39 | + |
| 40 | + pass |
| 41 | + |
| 42 | + |
| 43 | +class Mutable: |
| 44 | + """ |
| 45 | + A generic mutable type that can be marked immutable. |
| 46 | + """ |
| 47 | + |
| 48 | + def __init__(self, *args: Any, mutable: bool = True, **kwargs: Any): |
| 49 | + super().__init__(*args, **kwargs) |
| 50 | + self._mutable = mutable |
| 51 | + |
| 52 | + def get_mutable_instances(self) -> Iterable[Mutable]: |
| 53 | + """ |
| 54 | + Get any other :py:class:`betty.mutability.Mutable` instances contained by this one. |
| 55 | + """ |
| 56 | + return () |
| 57 | + |
| 58 | + @property |
| 59 | + def is_mutable(self) -> bool: |
| 60 | + """ |
| 61 | + Whether the instance is mutable. |
| 62 | + """ |
| 63 | + return self._mutable |
| 64 | + |
| 65 | + def mutable(self) -> None: |
| 66 | + """ |
| 67 | + Mark the instance mutable. |
| 68 | + """ |
| 69 | + self._mutable = True |
| 70 | + for instance in self.get_mutable_instances(): |
| 71 | + instance.mutable() |
| 72 | + |
| 73 | + @property |
| 74 | + def is_immutable(self) -> bool: |
| 75 | + """ |
| 76 | + Whether the instance is immutable. |
| 77 | + """ |
| 78 | + return not self._mutable |
| 79 | + |
| 80 | + def immutable(self) -> None: |
| 81 | + """ |
| 82 | + Mark the instance immutable. |
| 83 | + """ |
| 84 | + self._mutable = False |
| 85 | + for instance in self.get_mutable_instances(): |
| 86 | + instance.immutable() |
| 87 | + |
| 88 | + def assert_mutable(self) -> None: |
| 89 | + """ |
| 90 | + Assert that the instance is mutable. |
| 91 | +
|
| 92 | + :raise ImmutableError: if the instance is immutable. |
| 93 | + """ |
| 94 | + if not self._mutable: |
| 95 | + raise ImmutableError( |
| 96 | + f"{self} was unexpectedly immutable, and cannot be modified." |
| 97 | + ) |
| 98 | + |
| 99 | + def assert_immutable(self) -> None: |
| 100 | + """ |
| 101 | + Assert that the instance is immutable. |
| 102 | +
|
| 103 | + :raise MutableError: if the instance is mutable. |
| 104 | + """ |
| 105 | + if self._mutable: |
| 106 | + raise MutableError(f"{self} was unexpectedly mutable, and can be modified.") |
| 107 | + |
| 108 | + |
| 109 | +def mutable(*instances: Mutable) -> None: |
| 110 | + """ |
| 111 | + Mark the given instances mutable. |
| 112 | + """ |
| 113 | + for instance in instances: |
| 114 | + instance.mutable() |
| 115 | + |
| 116 | + |
| 117 | +def immutable(*instances: Mutable) -> None: |
| 118 | + """ |
| 119 | + Mark the given instances immutable. |
| 120 | + """ |
| 121 | + for instance in instances: |
| 122 | + instance.immutable() |
0 commit comments