Skip to content

fix: do not automatically generate header id in RDF patch generation and fix missing fullstop #3141

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

Merged
merged 7 commits into from
May 31, 2025
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
4 changes: 2 additions & 2 deletions rdflib/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,13 @@ def _traces(
elif best_score > color_score:
# prune this branch.
if stats is not None:
stats["prunings"] += 1 # type: ignore[operator]
stats["prunings"] = int(stats.get("prunings", 0)) + 1
elif experimental_score != best_experimental_score:
best.append(refined_coloring)
else:
# prune this branch.
if stats is not None:
stats["prunings"] += 1 # type: ignore[operator]
stats["prunings"] = int(stats.get("prunings", 0)) + 1
discrete: list[list[Color]] = [x for x in best if self._discrete(x)]
if len(discrete) == 0:
best_score = None
Expand Down
1 change: 1 addition & 0 deletions rdflib/plugins/parsers/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ def _add_list(
continue

if rest:
# type error: Statement is unreachable
graph.add((subj, RDF.rest, rest))
subj = rest

Expand Down
8 changes: 3 additions & 5 deletions rdflib/plugins/serializers/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import warnings
from typing import IO, Any
from uuid import uuid4

from rdflib import Dataset
from rdflib.plugins.serializers.nquads import _nq_row
Expand Down Expand Up @@ -51,8 +50,6 @@ def serialize(
target = kwargs.get("target")
header_id = kwargs.get("header_id")
header_prev = kwargs.get("header_prev")
if not header_id:
header_id = f"uuid:{uuid4()}"
encoding = self.encoding
if base is not None:
warnings.warn("PatchSerializer does not support base.")
Expand All @@ -63,9 +60,10 @@ def serialize(
)

def write_header():
stream.write(f"H id <{header_id}> .\n".encode(encoding, "replace"))
if header_id:
stream.write(f"H id <{header_id}> .\n".encode(encoding, "replace"))
if header_prev:
stream.write(f"H prev <{header_prev}>\n".encode(encoding, "replace"))
stream.write(f"H prev <{header_prev}> .\n".encode(encoding, "replace"))
stream.write("TX .\n".encode(encoding, "replace"))

def write_triples(contexts, op_code, use_passed_contexts=False):
Expand Down
4 changes: 4 additions & 0 deletions rdflib/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def register(self, object: Any, id: str) -> None:

def loads(self, s: bytes) -> Node:
up = Unpickler(BytesIO(s))
# NOTE on type error: https://github.com/python/mypy/issues/2427
# type error: Cannot assign to a method
up.persistent_load = self._get_object
try:
return up.load()
Expand All @@ -130,6 +132,8 @@ def loads(self, s: bytes) -> Node:
def dumps(self, obj: Node, protocol: Any | None = None, bin: Any | None = None):
src = BytesIO()
p = Pickler(src)
# NOTE on type error: https://github.com/python/mypy/issues/2427
# type error: Cannot assign to a method
p.persistent_id = self._get_ids
p.dump(obj)
return src.getvalue()
Expand Down
15 changes: 15 additions & 0 deletions test/test_serializers/test_serializer_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,18 @@ def test_prev_header():
)
result = ds.serialize(format="patch", operation="add", header_prev="uuid:123")
assert """H prev <uuid:123>""" in result


def test_no_headers():
ds = Dataset()
ds.add(
(
URIRef("http://example.org/subject1"),
URIRef("http://example.org/predicate2"),
Literal("object2"),
)
)
result = ds.serialize(format="patch", operation="add")
lines = result.split("\n")
for line in lines:
assert not line.startswith("H ")
Loading