Skip to content

Commit

Permalink
bunch is now readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
juanbc committed Aug 30, 2024
1 parent 3106edd commit 4bad2bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
13 changes: 7 additions & 6 deletions skcriteria/utils/bunch.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,14 @@ class Bunch(Mapping):
>>> b.a = 3
>>> b['a']
3
>>> b.c = 6
>>> b['c']
6
"""

def __init__(self, name, data):
if not isinstance(data, Mapping):
raise TypeError("Data must be some kind of mapping")
self._name = str(name)
self._data = data
super().__setattr__("_name", str(name))
super().__setattr__("_data", data)

def __getitem__(self, k):
"""x.__getitem__(y) <==> x[y]."""
Expand All @@ -69,6 +66,10 @@ def __getattr__(self, a):
except KeyError:
raise AttributeError(a)

def __setattr__(self, a, v):
"""x.__setattr__(a, v) <==> x.a = v."""
raise AttributeError(f"Bunch {self._name!r} is read-only")

def __copy__(self):
"""x.__copy__() <==> copy.copy(x)."""
cls = type(self)
Expand All @@ -87,7 +88,7 @@ def __deepcopy__(self, memo):
memo[id(self)] = clone

# now we copy the data
clone._data = copy.deepcopy(self._data, memo)
super(cls, clone).__setattr__("_data", copy.deepcopy(self._data, memo))

return clone

Expand Down
6 changes: 6 additions & 0 deletions tests/utils/test_bunch.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,9 @@ def test_Bunch_copy():
def test_Bunch_data_is_not_a_mapping():
with pytest.raises(TypeError, match="Data must be some kind of mapping"):
bunch.Bunch("foo", None)


def test_Bunch_assign_fails():
foo_bunch = bunch.Bunch("foo", {})
with pytest.raises(AttributeError, match="Bunch 'foo' is read-only"):
foo_bunch.some_key = 1

0 comments on commit 4bad2bf

Please sign in to comment.