Skip to content
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

Fix 939 return cg from parse #2563

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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 @@ -2216,7 +2216,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).
Expand Down Expand Up @@ -2272,8 +2272,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 @@ -2476,7 +2475,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.

Expand Down Expand Up @@ -2512,8 +2511,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]]
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
20 changes: 20 additions & 0 deletions test/test_conjunctivegraph/test_conjunctive_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""


from io import StringIO

import pytest

from rdflib import ConjunctiveGraph, Graph
Expand Down Expand Up @@ -77,3 +79,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
22 changes: 22 additions & 0 deletions test/test_dataset/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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

Expand All @@ -25,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 @@ -262,3 +266,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
Loading