From 092b1863d5a033d4bd490c3d1fd33c1cbb7379e7 Mon Sep 17 00:00:00 2001 From: "i.nueske" <i.nueske@indiscale.com> Date: Tue, 14 Jan 2025 10:59:13 +0100 Subject: [PATCH] ENH: Update add_property to prevent overwriting special attributes --- src/linkahead/common/models.py | 13 +++++++++++-- unittests/test_issues.py | 5 +++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py index 0b66acd9..35db9f62 100644 --- a/src/linkahead/common/models.py +++ b/src/linkahead/common/models.py @@ -751,8 +751,17 @@ class Entity: # If the name is a special attribute, set the attribute instead if name in SPECIAL_ATTRIBUTES and name in dir(self): - setattr(self, name, value) - return self + if getattr(self, name) is None: + setattr(self, name, value) + return self + else: + raise ValueError(f"'{name}' is a special attribute and does not " + f"support multi-property. It is already set " + f"and cannot be overwritten using this method. " + f"Please use direct assignment for setting this " + f"property after the first time.") + # ToDo: Implement the same behaviour for special attribute ids, + # https://gitlab.indiscale.com/caosdb/src/caosdb-pylib/-/issues/219 new_property = Property(name=name, id=id, description=description, datatype=datatype, value=value, unit=unit) diff --git a/unittests/test_issues.py b/unittests/test_issues.py index 5f44a904..33fdc9e6 100644 --- a/unittests/test_issues.py +++ b/unittests/test_issues.py @@ -143,3 +143,8 @@ def test_issue_134(): rec.add_property(name=attr, value=val) assert rec.__getattribute__(attr) == val assert rec.get_property(attr) is None + + exp_str = f"'{attr}' is a special attribute and does not support" + with raises(ValueError) as e: + rec.add_property(name=attr, value=val) + assert exp_str in str(e.value) -- GitLab