From 7485882997ad3b11219a86ddb2a5c0065b81d431 Mon Sep 17 00:00:00 2001 From: Edmond Chuc Date: Fri, 17 Oct 2025 15:41:53 +1000 Subject: [PATCH 1/2] fix: Dataset.parse now returns Self --- rdflib/graph.py | 8 +++----- rdflib/plugins/parsers/nquads.py | 2 +- test/test_dataset/test_dataset_add.py | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/rdflib/graph.py b/rdflib/graph.py index bc9ab05f2..90907cde0 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -2346,8 +2346,7 @@ def parse( context = self.default_context context.parse(source, publicID=publicID, format=format, **args) - # TODO: FIXME: This should not return context, but self. - return context + return self def __reduce__(self) -> Tuple[Type[Graph], Tuple[Store, _ContextIdentifierType]]: return ConjunctiveGraph, (self.store, self.identifier) @@ -2615,11 +2614,10 @@ def parse( (i.e. :attr:`.Dataset.default_context`). """ - c = ConjunctiveGraph.parse( + ConjunctiveGraph.parse( self, source, publicID, format, location, file, data, **args ) - self.graph(c) - return c + return self def add_graph( self, g: Optional[Union[_ContextIdentifierType, _ContextType, str]] diff --git a/rdflib/plugins/parsers/nquads.py b/rdflib/plugins/parsers/nquads.py index 60b793b65..cadcfa28a 100644 --- a/rdflib/plugins/parsers/nquads.py +++ b/rdflib/plugins/parsers/nquads.py @@ -7,7 +7,7 @@ >>> g = ConjunctiveGraph() >>> data = open("test/data/nquads.rdflib/example.nquads", "rb") >>> g.parse(data, format="nquads") # doctest:+ELLIPSIS -)> +)> >>> assert len(g.store) == 449 >>> # There should be 16 separate contexts >>> assert len([x for x in g.store.contexts()]) == 16 diff --git a/test/test_dataset/test_dataset_add.py b/test/test_dataset/test_dataset_add.py index e2b587613..161dda247 100644 --- a/test/test_dataset/test_dataset_add.py +++ b/test/test_dataset/test_dataset_add.py @@ -1,4 +1,5 @@ from rdflib import RDF, RDFS, Dataset, Graph, URIRef +from test.data import TEST_DATA_DIR def test_behaviour_where_graph_is_created_via_dataset(): @@ -86,3 +87,28 @@ def test_adding_appends_to_dataset_graph(): graph = ds.add_graph(another_graph) assert len(graph) == 3 assert len(ds) == 3 + + +def test_dataset_parse_return_value(): + """ + Test that the return value of ds.parse has the same reference as ds. + """ + ds = Dataset() + return_value = ds.parse( + source=TEST_DATA_DIR / "nquads.rdflib/example.nquads", format="nquads" + ) + assert len(ds) + assert return_value is ds + + +def test_dataset_graphs_excludes_default_graph(): + """ + The Dataset.graphs() method should exclude the default graph. + """ + ds = Dataset() + ds.parse(source=TEST_DATA_DIR / "nquads.rdflib/example.nquads", format="nquads") + assert len(ds) + # There are 16 named graphs. Calling Dataset.graphs() with no triple pattern should + # return only 16 results and exclude the default graph. + # TODO: update this when Dataset.graphs() is updated to exclude the default graph. + assert len(list(ds.graphs())) == 17 From 4f2924b98c8ea75d2ce9507d4cd8a9f59027d3d5 Mon Sep 17 00:00:00 2001 From: Edmond Chuc Date: Fri, 17 Oct 2025 15:56:04 +1000 Subject: [PATCH 2/2] chore: remove test that's not relevant for this PR --- test/test_dataset/test_dataset_add.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/test/test_dataset/test_dataset_add.py b/test/test_dataset/test_dataset_add.py index 161dda247..a3197ae2e 100644 --- a/test/test_dataset/test_dataset_add.py +++ b/test/test_dataset/test_dataset_add.py @@ -99,16 +99,3 @@ def test_dataset_parse_return_value(): ) assert len(ds) assert return_value is ds - - -def test_dataset_graphs_excludes_default_graph(): - """ - The Dataset.graphs() method should exclude the default graph. - """ - ds = Dataset() - ds.parse(source=TEST_DATA_DIR / "nquads.rdflib/example.nquads", format="nquads") - assert len(ds) - # There are 16 named graphs. Calling Dataset.graphs() with no triple pattern should - # return only 16 results and exclude the default graph. - # TODO: update this when Dataset.graphs() is updated to exclude the default graph. - assert len(list(ds.graphs())) == 17