Skip to content

Commit

Permalink
enforce entities having an ID before they get properties, fixes #129.
Browse files Browse the repository at this point in the history
  • Loading branch information
pudo committed Oct 30, 2023
1 parent ec28c4e commit c6570f2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 2 additions & 0 deletions nomenklatura/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ def unsafe_add(
if original_value is None and clean != value:
original_value = value

if self.id is None:
raise InvalidData("Cannot add statement to entity without ID!")
stmt = Statement(
entity_id=self.id,
prop=prop.name,
Expand Down
14 changes: 7 additions & 7 deletions nomenklatura/statement/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Statement(object):

def __init__(
self,
entity_id: Optional[str],
entity_id: str,
prop: str,
schema: str,
value: str,
Expand Down Expand Up @@ -95,10 +95,8 @@ def __init__(
self.id = id

def to_dict(self) -> StatementDict:
if self.entity_id is None:
raise ValueError("Statement has no entity ID!")
return {
"canonical_id": self.canonical_id or self.entity_id,
"canonical_id": self.canonical_id,
"entity_id": self.entity_id,
"prop": self.prop,
"prop_type": self.prop_type,
Expand Down Expand Up @@ -127,7 +125,9 @@ def to_db_row(self) -> Dict[str, Any]:
return data

def __hash__(self) -> int:
return hash(self.id)
if self.id is not None:
return hash(self.id)
return hash(self.generate_key())

def __repr__(self) -> str:
return "<Statement(%r, %r, %r)>" % (self.entity_id, self.prop, self.value)
Expand Down Expand Up @@ -157,13 +157,13 @@ def generate_key(self) -> Optional[str]:
def make_key(
cls,
dataset: str,
entity_id: Optional[str],
entity_id: str,
prop: str,
value: str,
external: Optional[bool],
) -> Optional[str]:
"""Hash the key properties of a statement record to make a unique ID."""
if entity_id is None or prop is None:
if prop is None or value is None:
return None
key = f"{dataset}.{entity_id}.{prop}.{value}"
if external:
Expand Down

0 comments on commit c6570f2

Please sign in to comment.