Skip to content

Commit 7f2bbc6

Browse files
Merge pull request #66 from apdavison/allow-type-in-list
Accept `@type` as a list containing a single IRI
2 parents 29ca99f + 6085218 commit 7f2bbc6

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

pipeline/src/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ def from_jsonld(cls, data, ignore_unexpected_keys=False):
123123
data_copy = data.copy()
124124
context = data_copy.pop("@context", None)
125125
type_ = data_copy.pop("@type")
126+
if isinstance(type_, list) and len(type_) == 1:
127+
type_ = type_[0]
126128
if type_ and type_ != cls.type_:
127129
raise TypeError(f"Mismatched types. Data has '{type_}' but trying to create '{cls.type_}'")
128130
deserialized_data = {}

pipeline/src/properties.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,21 @@ def deserialize_item(item):
214214
elif all(issubclass(t, Node) for t in self.types):
215215
# use data["@type"] to figure out class to use
216216
if "@type" in item:
217+
if isinstance(item["@type"], list) and len(item["@type"]) == 1:
218+
item_type = item["@type"][0]
219+
else:
220+
item_type = item["@type"]
217221
for cls in self.types:
218-
if cls.type_ == item["@type"]:
222+
if cls.type_ == item_type:
219223
if set(item.keys()) == link_keys:
220224
# if we only have @id and @type, it's a Link
221225
return Link(item["@id"], allowed_types=[cls])
222226
else:
223227
# otherwise it's a Node
224228
return cls.from_jsonld(item)
225229
raise TypeError(
226-
f"Mismatched types. Data has '{item['@type']}' "
227-
f"but property only allows {[cls.type_ for cls in self.types]}"
230+
f"Mismatched types. Data has '{item_type}' "
231+
f"but property only allows one of {[cls.type_ for cls in self.types]}"
228232
)
229233
else:
230234
return Link(item["@id"], allowed_types=self.types)

0 commit comments

Comments
 (0)