Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Entity id a readonly property #169

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions rocrate/model/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@ 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)
self._jsonld = empty
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):
Expand Down
7 changes: 7 additions & 0 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading