diff --git a/rocrate/model/entity.py b/rocrate/model/entity.py index 41bf09b..fa26218 100644 --- a/rocrate/model/entity.py +++ b/rocrate/model/entity.py @@ -30,9 +30,9 @@ class Entity(MutableMapping): def __init__(self, crate, identifier=None, properties=None): self.crate = crate if identifier: - self.id = self.format_id(identifier) + self.__id = self.format_id(identifier) else: - self.id = f"#{uuid.uuid4()}" + self.__id = f"#{uuid.uuid4()}" if properties: empty = self._empty() empty.update(properties) @@ -40,6 +40,10 @@ def __init__(self, crate, identifier=None, properties=None): else: self._jsonld = self._empty() + @property + def id(self): + return self.__id + # Format the given ID with rules appropriate for this type. # For example, Dataset (directory) data entities SHOULD end with / def format_id(self, identifier): diff --git a/test/test_model.py b/test/test_model.py index e7b5343..9f00a48 100644 --- a/test/test_model.py +++ b/test/test_model.py @@ -505,3 +505,10 @@ def test_add_no_duplicates(test_data_dir, tmpdir): assert ret["name"] == "Jim" assert ret in crate.get_entities() assert crate.contextual_entities == [jim] + + +def test_immutable_id(): + crate = ROCrate() + p = crate.add(Person(crate, "#foo")) + with pytest.raises(AttributeError): + p.id = "#bar"