diff --git a/rdflib/graph.py b/rdflib/graph.py index 9ba3dd396..d88fe0ac1 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -2349,7 +2349,7 @@ def parse( file: BinaryIO | TextIO | None = None, data: str | bytes | None = None, **args: Any, - ) -> Graph: + ) -> "ConjunctiveGraph": """ Parse source adding the resulting triples to its own context (sub graph of this graph). @@ -2406,8 +2406,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) @@ -2644,7 +2643,7 @@ def parse( file: BinaryIO | TextIO | None = None, data: str | bytes | None = None, **args: Any, - ) -> Graph: + ) -> "Dataset": """ Parse an RDF source adding the resulting triples to the Graph. @@ -2681,8 +2680,11 @@ def parse( c = ConjunctiveGraph.parse( self, source, publicID, format, location, file, data, **args ) - self.graph(c) - return c + + for context in c.contexts(): + self.graph(context) + + return self def add_graph(self, g: _ContextIdentifierType | _ContextType | str | None) -> Graph: """alias of graph for consistency""" diff --git a/rdflib/plugins/parsers/nquads.py b/rdflib/plugins/parsers/nquads.py index 2ed2ab4a1..11815be42 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_conjunctivegraph/test_conjunctive_graph.py b/test/test_conjunctivegraph/test_conjunctive_graph.py index 0fdb0af1d..78c6f6821 100644 --- a/test/test_conjunctivegraph/test_conjunctive_graph.py +++ b/test/test_conjunctivegraph/test_conjunctive_graph.py @@ -2,6 +2,7 @@ Tests for ConjunctiveGraph that do not depend on the underlying store """ +from io import StringIO import pytest from rdflib import ConjunctiveGraph, Graph @@ -83,3 +84,21 @@ def check(kws): @pytest.mark.parametrize("checker, kws", get_graph_ids_tests()) def test_graph_ids(checker, kws): checker(kws) + + +def test_parse_return_type(): + g = ConjunctiveGraph() + g.parse(data=DATA, format="turtle") + assert type(g) is ConjunctiveGraph + + g = ConjunctiveGraph() + g = g.parse(data=DATA, format="turtle") + assert type(g) is ConjunctiveGraph + + g = ConjunctiveGraph() + g.parse(source=StringIO(DATA), format="turtle") + assert type(g) is ConjunctiveGraph + + g = ConjunctiveGraph() + g = g.parse(source=StringIO(DATA), format="turtle") + assert type(g) is ConjunctiveGraph diff --git a/test/test_dataset/test_dataset.py b/test/test_dataset/test_dataset.py index 9f9bc9c26..36b1c1cf8 100644 --- a/test/test_dataset/test_dataset.py +++ b/test/test_dataset/test_dataset.py @@ -1,7 +1,9 @@ import os import shutil import tempfile -import warnings +from io import StringIO +from test.data import CONTEXT1, LIKES, PIZZA, TAREK +from test.utils.namespace import EGSCHEME import pytest @@ -24,6 +26,9 @@ HOST = "http://localhost:3030" DB = "/db/" +DATA = """ + a . +""" pluginstores = [] @@ -270,6 +275,25 @@ def test_graph_without_identifier() -> None: ) == ("genid", genid_prefix) assert f"{g1.identifier}".startswith(genid_prefix) + assert f"{subgraph.identifier}".startswith(genid_prefix) + + +def test_parse_return_type(): + g = Dataset() + g.parse(data=DATA, format="turtle") + assert type(g) is Dataset + + g = Dataset() + g = g.parse(data=DATA, format="turtle") + assert type(g) is Dataset + + g = Dataset() + g.parse(source=StringIO(DATA), format="turtle") + assert type(g) is Dataset + + g = Dataset() + g = g.parse(source=StringIO(DATA), format="turtle") + assert type(g) is Dataset # now add a preexisting graph with no identifier # i.e. not one created within this Dataset object