Skip to content

Commit 01eb723

Browse files
author
Sergio García Prado
committed
ISSUE #?
* Fix bug related with setter.
1 parent e8aac78 commit 01eb723

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

packages/core/minos-microservice-aggregate/minos/aggregate/entities/refs/models.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ def __getitem__(self, item: str) -> Any:
8989
except Exception:
9090
raise exc
9191

92+
def __setattr__(self, key: str, value: Any) -> None:
93+
try:
94+
return super().__setattr__(key, value)
95+
except AttributeError as exc:
96+
if key == "uuid":
97+
self.data = value
98+
return
99+
100+
try:
101+
setattr(self.data, key, value)
102+
except Exception:
103+
raise exc
104+
92105
def __getattr__(self, item: str) -> Any:
93106
try:
94107
return super().__getattr__(item)
@@ -183,14 +196,6 @@ def uuid(self) -> UUID:
183196
return self.data
184197
return self.data.uuid
185198

186-
@uuid.setter
187-
def uuid(self, value: UUID) -> None:
188-
"""Set the uuid that identifies the ``Model``.
189-
190-
:return: This method does not return anything.
191-
"""
192-
self.data = value
193-
194199
@property
195200
def data_cls(self) -> Optional[type]:
196201
"""Get data class if available.

packages/core/minos-microservice-aggregate/minos/aggregate/events/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ def __getitem__(self, item: str) -> Any:
7676
raise exc
7777
raise exc
7878

79+
def __getattr__(self, item: str) -> Any:
80+
try:
81+
return super().__getattr__(item)
82+
except AttributeError as exc:
83+
try:
84+
return self[item]
85+
except Exception:
86+
raise exc
87+
7988
def get_one(self, name: str, return_diff: bool = False) -> Union[FieldDiff, Any, list[FieldDiff], list[Any]]:
8089
"""Get first field diff with given name.
8190

packages/core/minos-microservice-common/minos/common/model/abc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,16 @@ def __setattr__(self, key: str, value: Any) -> None:
240240
object.__setattr__(self, key, value)
241241
return
242242

243+
if key not in self._fields:
244+
raise AttributeError(f"{type(self).__name__!r} does not contain the {key!r} attribute.")
245+
243246
try:
244247
self[key] = value
245248
except KeyError as exc:
246249
raise AttributeError(str(exc))
247250

248251
def __getattr__(self, item: str) -> Any:
249-
if item.startswith("_"):
252+
if item.startswith("_") or item not in self._fields:
250253
raise AttributeError(f"{type(self).__name__!r} does not contain the {item!r} attribute.")
251254

252255
try:

packages/core/minos-microservice-saga/minos/saga/context.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ def __delitem__(self, item: str) -> None:
3737
except KeyError:
3838
raise KeyError(f"{type(self).__name__!r} does not contain the {item!r} field")
3939

40+
def __setattr__(self, key: str, value: Any) -> None:
41+
try:
42+
super().__setattr__(key, value)
43+
except AttributeError:
44+
self[key] = value
45+
4046
def __delattr__(self, item: str) -> None:
4147
if item.startswith("_"):
4248
super().__delattr__(item)
4349
return
4450

4551
try:
46-
self[item]
52+
del self[item]
4753
except KeyError as exc:
4854
raise AttributeError(str(exc))

packages/core/minos-microservice-saga/tests/test_saga/test_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_deleter_reserved_word(self):
5757
context["items"] = "foo"
5858
del context.items
5959
self.assertIsInstance(context.items, Callable)
60-
self.assertEqual("foo", context.fields["items"].value)
60+
self.assertNotIn("items", context.fields)
6161

6262
def test_deleter_raises(self):
6363
with self.assertRaises(AttributeError):

0 commit comments

Comments
 (0)