Skip to content

Fix 939 return cg from parse #2563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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"""
Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugins/parsers/nquads.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
>>> g = ConjunctiveGraph()
>>> data = open("test/data/nquads.rdflib/example.nquads", "rb")
>>> g.parse(data, format="nquads") # doctest:+ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
<Graph identifier=... (<class 'rdflib.graph.ConjunctiveGraph'>)>
>>> assert len(g.store) == 449
>>> # There should be 16 separate contexts
>>> assert len([x for x in g.store.contexts()]) == 16
Expand Down
19 changes: 19 additions & 0 deletions test/test_conjunctivegraph/test_conjunctive_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
26 changes: 25 additions & 1 deletion test/test_dataset/test_dataset.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -24,6 +26,9 @@

HOST = "http://localhost:3030"
DB = "/db/"
DATA = """
<http://example.org/record/1> a <http://xmlns.com/foaf/0.1/Document> .
"""

pluginstores = []

Expand Down Expand Up @@ -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
Expand Down
Loading