diff --git a/rdflib/graph.py b/rdflib/graph.py index a8b1593ea..aaacc2d32 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -2205,7 +2205,7 @@ def parse( file: Optional[Union[BinaryIO, TextIO]] = None, data: Optional[Union[str, bytes]] = None, **args: Any, - ) -> Graph: + ) -> "ConjunctiveGraph": """ Parse source adding the resulting triples to its own context (sub graph of this graph). @@ -2261,8 +2261,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) @@ -2465,7 +2464,7 @@ def parse( file: Optional[Union[BinaryIO, TextIO]] = None, data: Optional[Union[str, bytes]] = None, **args: Any, - ) -> Graph: + ) -> "Dataset": """ Parse an RDF source adding the resulting triples to the Graph. @@ -2501,8 +2500,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: Optional[Union[_ContextIdentifierType, _ContextType, str]] diff --git a/rdflib/plugins/parsers/nquads.py b/rdflib/plugins/parsers/nquads.py index 51a6c65b1..5f3e791b8 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 19992a064..9d341b895 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 @@ -76,3 +77,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 a6e005c08..3de81f329 100644 --- a/test/test_dataset/test_dataset.py +++ b/test/test_dataset/test_dataset.py @@ -1,6 +1,7 @@ import os import shutil import tempfile +from io import StringIO from test.data import CONTEXT1, LIKES, PIZZA, TAREK from test.utils.namespace import EGSCHEME @@ -24,6 +25,9 @@ HOST = "http://localhost:3030" DB = "/db/" +DATA = """ + a . +""" pluginstores = [] @@ -261,3 +265,21 @@ def test_subgraph_without_identifier() -> None: ) == ("genid", 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