From db659fea7c6343cff7cdfda4da6efb0cdc2be326 Mon Sep 17 00:00:00 2001 From: recalcitrantsupplant Date: Mon, 26 May 2025 14:24:11 +1000 Subject: [PATCH 1/5] fix: do not automatically generate header id in RDF patch generation. Fix bug with missing header previous fullstop/period at end of line. --- rdflib/plugins/serializers/patch.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rdflib/plugins/serializers/patch.py b/rdflib/plugins/serializers/patch.py index 58928c6a0..f3fadf5db 100644 --- a/rdflib/plugins/serializers/patch.py +++ b/rdflib/plugins/serializers/patch.py @@ -51,8 +51,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.") @@ -63,9 +61,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): From c60680bf1db40fec4e17b7e951cd422f09250cec Mon Sep 17 00:00:00 2001 From: recalcitrantsupplant Date: Mon, 26 May 2025 14:41:33 +1000 Subject: [PATCH 2/5] fix: add basic test that header is not added when not specified --- test/test_serializers/test_serializer_patch.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/test_serializers/test_serializer_patch.py b/test/test_serializers/test_serializer_patch.py index 6d8a05055..671f1ba91 100644 --- a/test/test_serializers/test_serializer_patch.py +++ b/test/test_serializers/test_serializer_patch.py @@ -176,3 +176,17 @@ def test_prev_header(): ) result = ds.serialize(format="patch", operation="add", header_prev="uuid:123") assert """H prev """ 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 ") From 4cbeb8bcf96d7a537ca0a48f2bed9d35d2e30bb7 Mon Sep 17 00:00:00 2001 From: recalcitrantsupplant Date: Mon, 26 May 2025 14:51:35 +1000 Subject: [PATCH 3/5] remove unused import --- rdflib/plugins/serializers/patch.py | 1 - 1 file changed, 1 deletion(-) diff --git a/rdflib/plugins/serializers/patch.py b/rdflib/plugins/serializers/patch.py index f3fadf5db..da32ead97 100644 --- a/rdflib/plugins/serializers/patch.py +++ b/rdflib/plugins/serializers/patch.py @@ -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 From bbce67de0fab1a34d11ff39643ce09d648c44cdc Mon Sep 17 00:00:00 2001 From: recalcitrantsupplant Date: Tue, 27 May 2025 12:58:48 +1000 Subject: [PATCH 4/5] black code --- test/test_serializers/test_serializer_patch.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_serializers/test_serializer_patch.py b/test/test_serializers/test_serializer_patch.py index 671f1ba91..c0c44ca87 100644 --- a/test/test_serializers/test_serializer_patch.py +++ b/test/test_serializers/test_serializer_patch.py @@ -177,6 +177,7 @@ def test_prev_header(): result = ds.serialize(format="patch", operation="add", header_prev="uuid:123") assert """H prev """ in result + def test_no_headers(): ds = Dataset() ds.add( From a8e3ed834779b43efa5ee4629cbd0a8950666e00 Mon Sep 17 00:00:00 2001 From: recalcitrantsupplant Date: Tue, 27 May 2025 13:56:47 +1000 Subject: [PATCH 5/5] fix mypy errors --- rdflib/compare.py | 12 ++++++------ rdflib/plugins/parsers/jsonld.py | 2 +- rdflib/store.py | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/rdflib/compare.py b/rdflib/compare.py index 58644ae8f..7c808b1a5 100644 --- a/rdflib/compare.py +++ b/rdflib/compare.py @@ -442,24 +442,24 @@ def _traces( experimental = self._experimental_path(coloring_copy) experimental_score = set([c.key() for c in experimental]) if last_coloring: - generator = self._create_generator( # type: ignore[unreachable] + generator = self._create_generator( [last_coloring, experimental], generator ) last_coloring = experimental - if best_score is None or best_score < color_score: # type: ignore[unreachable] + if best_score is None or best_score < color_score: best = [refined_coloring] best_score = color_score best_experimental_score = experimental_score - elif best_score > color_score: # type: ignore[unreachable] + elif best_score > color_score: # prune this branch. if stats is not None: - stats["prunings"] += 1 + 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 + 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 @@ -468,7 +468,7 @@ def _traces( d = [depth[0]] new_color = self._traces(coloring, stats=stats, depth=d) color_score = tuple([c.key() for c in refined_coloring]) - if best_score is None or color_score > best_score: # type: ignore[unreachable] + if best_score is None or color_score > best_score: discrete = [new_color] best_score = color_score best_depth = d[0] diff --git a/rdflib/plugins/parsers/jsonld.py b/rdflib/plugins/parsers/jsonld.py index 45e696adb..0031c8662 100644 --- a/rdflib/plugins/parsers/jsonld.py +++ b/rdflib/plugins/parsers/jsonld.py @@ -663,7 +663,7 @@ def _add_list( if rest: # type error: Statement is unreachable - graph.add((subj, RDF.rest, rest)) # type: ignore[unreachable] + graph.add((subj, RDF.rest, rest)) subj = rest obj = self._to_object(dataset, graph, context, term, node, inlist=True) diff --git a/rdflib/store.py b/rdflib/store.py index 96a16956b..51d6d8422 100644 --- a/rdflib/store.py +++ b/rdflib/store.py @@ -123,7 +123,7 @@ 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 # type: ignore[assignment] + up.persistent_load = self._get_object try: return up.load() except KeyError as e: @@ -134,7 +134,7 @@ def dumps(self, obj: Node, protocol: Any | None = None, bin: Any | None = None): 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 # type: ignore[assignment] + p.persistent_id = self._get_ids p.dump(obj) return src.getvalue()