Skip to content

Commit 32fb892

Browse files
committed
fix!: Fix cs.Parts .name attribute setting
Setting a name on the `Part` is only allowed during creation. Else this won't have any effect since its inferred from its `.type`. The user should be informed about it.
1 parent 66058ae commit 32fb892

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

capellambse/model/common/accessors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"ReferenceSearchingAccessor",
2323
"ElementListCouplingMixin",
2424
"RoleTagAccessor",
25+
"InvalidChangeRequest",
2526
]
2627

2728
import abc
@@ -46,6 +47,10 @@
4647
_C = t.TypeVar("_C", bound="ElementListCouplingMixin")
4748

4849

50+
class InvalidChangeRequest(Exception):
51+
"""Raised when a change request is invalid."""
52+
53+
4954
class NonUniqueMemberError(ValueError):
5055
"""Raised when a duplicate member is inserted into a list."""
5156

capellambse/model/common/element.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,20 @@ def __init__(
211211
parent.append(self._element)
212212
try:
213213
for key, val in kw.items():
214+
prop = getattr(type(self), key)
215+
is_acc_or_attr_prop = isinstance(
216+
prop, (accessors.Accessor, properties.AttributeProperty)
217+
)
214218
if key == "xtype":
215219
self._element.set(helpers.ATT_XT, val)
216-
elif not isinstance(
217-
getattr(type(self), key),
218-
(accessors.Accessor, properties.AttributeProperty),
220+
elif is_acc_or_attr_prop or (
221+
isinstance(prop, property) and not self._constructed
219222
):
223+
setattr(self, key, val)
224+
else:
220225
raise TypeError(
221226
f"Cannot set {key!r} on {type(self).__name__}"
222227
)
223-
else:
224-
setattr(self, key, val)
225228
self._model._loader.idcache_index(self._element)
226229
except BaseException:
227230
parent.remove(self._element)

capellambse/model/crosslayer/cs.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,30 @@ class Part(c.GenericElement):
3838

3939
deployed_parts: c.Accessor
4040

41-
@property
42-
def name(self) -> str: # type: ignore[override]
41+
@property # type: ignore[override]
42+
def name(self) -> str:
43+
"""Return the name of the Part."""
4344
return self.type.name
4445

46+
@name.setter
47+
def name(self, value: str) -> None:
48+
if not isinstance(value, str):
49+
raise TypeError("Name has to be a string")
50+
51+
if self._constructed:
52+
raise c.InvalidChangeRequest(
53+
"This won't have any effect. The name is inferred from "
54+
"`.type`."
55+
)
56+
57+
if not value:
58+
try:
59+
del self._element.attrib["name"]
60+
except KeyError:
61+
pass
62+
else:
63+
self._element.attrib["name"] = value
64+
4565

4666
@c.xtype_handler(None)
4767
class ExchangeItemAllocation(c.GenericElement):

0 commit comments

Comments
 (0)