Skip to content

Commit 0f3237d

Browse files
authored
fix: Dataset.parse now returns Self (#3263)
* fix: Dataset.parse now returns Self * chore: remove test that's not relevant for this PR
1 parent 1a20cd9 commit 0f3237d

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

rdflib/graph.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,8 +2346,7 @@ def parse(
23462346

23472347
context = self.default_context
23482348
context.parse(source, publicID=publicID, format=format, **args)
2349-
# TODO: FIXME: This should not return context, but self.
2350-
return context
2349+
return self
23512350

23522351
def __reduce__(self) -> Tuple[Type[Graph], Tuple[Store, _ContextIdentifierType]]:
23532352
return ConjunctiveGraph, (self.store, self.identifier)
@@ -2615,11 +2614,10 @@ def parse(
26152614
(i.e. :attr:`.Dataset.default_context`).
26162615
"""
26172616

2618-
c = ConjunctiveGraph.parse(
2617+
ConjunctiveGraph.parse(
26192618
self, source, publicID, format, location, file, data, **args
26202619
)
2621-
self.graph(c)
2622-
return c
2620+
return self
26232621

26242622
def add_graph(
26252623
self, g: Optional[Union[_ContextIdentifierType, _ContextType, str]]

rdflib/plugins/parsers/nquads.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
>>> g = ConjunctiveGraph()
88
>>> data = open("test/data/nquads.rdflib/example.nquads", "rb")
99
>>> g.parse(data, format="nquads") # doctest:+ELLIPSIS
10-
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
10+
<Graph identifier=... (<class 'rdflib.graph.ConjunctiveGraph'>)>
1111
>>> assert len(g.store) == 449
1212
>>> # There should be 16 separate contexts
1313
>>> assert len([x for x in g.store.contexts()]) == 16

test/test_dataset/test_dataset_add.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from rdflib import RDF, RDFS, Dataset, Graph, URIRef
2+
from test.data import TEST_DATA_DIR
23

34

45
def test_behaviour_where_graph_is_created_via_dataset():
@@ -86,3 +87,15 @@ def test_adding_appends_to_dataset_graph():
8687
graph = ds.add_graph(another_graph)
8788
assert len(graph) == 3
8889
assert len(ds) == 3
90+
91+
92+
def test_dataset_parse_return_value():
93+
"""
94+
Test that the return value of ds.parse has the same reference as ds.
95+
"""
96+
ds = Dataset()
97+
return_value = ds.parse(
98+
source=TEST_DATA_DIR / "nquads.rdflib/example.nquads", format="nquads"
99+
)
100+
assert len(ds)
101+
assert return_value is ds

0 commit comments

Comments
 (0)