From 2593ae72e2ac94b6b43694c51b866776582c3f45 Mon Sep 17 00:00:00 2001 From: Tpt Date: Sat, 30 Mar 2024 17:32:03 +0100 Subject: [PATCH] Store.triples: Avoids to raise an exception in case of invalid triple pattern Issue #42 --- .pre-commit-config.yaml | 4 ++-- oxrdflib/__init__.py | 15 +++++++++------ pyproject.toml | 1 - tests/test_store.py | 8 ++++++++ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2a74a7f..f311adc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,12 +24,12 @@ repos: - id: requirements-txt-fixer - id: trailing-whitespace - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.2.2 + rev: v0.3.4 hooks: - id: ruff-format - id: ruff args: [--fix, --exit-non-zero-on-fix] - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.8.0 + rev: v1.9.0 hooks: - id: mypy diff --git a/oxrdflib/__init__.py b/oxrdflib/__init__.py index 7001baa..84e2fc8 100644 --- a/oxrdflib/__init__.py +++ b/oxrdflib/__init__.py @@ -86,13 +86,16 @@ def triples( triple_pattern: _TriplePattern, context: Optional[Graph] = None, ) -> Iterator[Tuple[_Triple, Iterator[Optional[Graph]]]]: - return ( - ( - (_from_ox(q.subject), _from_ox(q.predicate), _from_ox(q.object)), - iter(((_from_ox_graph_name(q.graph_name, self) if q.graph_name != ox.DefaultGraph() else None),)), + try: + return ( + ( + (_from_ox(q.subject), _from_ox(q.predicate), _from_ox(q.object)), + iter(((_from_ox_graph_name(q.graph_name, self) if q.graph_name != ox.DefaultGraph() else None),)), + ) + for q in self._inner.quads_for_pattern(*_to_ox_quad_pattern(triple_pattern, context)) ) - for q in self._inner.quads_for_pattern(*_to_ox_quad_pattern(triple_pattern, context)) - ) + except (TypeError, ValueError): + return iter(()) # We just don't return anything def __len__(self, context: Optional[Graph] = None) -> int: if context is None: diff --git a/pyproject.toml b/pyproject.toml index 62cc554..dbc6f96 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -56,7 +56,6 @@ select = [ "FLY", "I", "ICN", - "ISC", "N", "PERF", "PIE", diff --git a/tests/test_store.py b/tests/test_store.py index d3ec052..8ecd304 100644 --- a/tests/test_store.py +++ b/tests/test_store.py @@ -42,6 +42,14 @@ def test_store_with_shared_backend(self): self._fill_graph(Graph(store=OxigraphStore(store=store), identifier="http://example.com")) self._test_graph(Graph(store=OxigraphStore(store=store), identifier="http://example.com")) + def test_json_serialization(self): + graph1 = Graph("Oxigraph", identifier=EX.graph) + graph1.add((EX.foo, EX.name, Literal("foo"))) + json_ld = graph1.serialize(format="json-ld") + graph2 = Graph("Oxigraph", identifier=EX.graph) + graph2.parse(data=json_ld, format="json-ld") + self.assertEqual(graph1, graph2) + @staticmethod def _fill_graph(g: Graph): g.add((EX.foo, RDF.type, EX.Entity))