diff --git a/rdflib/graph.py b/rdflib/graph.py index a5cd98818..6001c55d8 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -1817,7 +1817,7 @@ def do_de_skolemize2(t: _TripleType) -> _TripleType: return retval def cbd( - self, resource: _SubjectType, target_graph: Optional[Graph] = None + self, resource: _SubjectType, *, target_graph: Optional[Graph] = None ) -> Graph: """Retrieves the Concise Bounded Description of a Resource from a Graph diff --git a/rdflib/plugins/sparql/evaluate.py b/rdflib/plugins/sparql/evaluate.py index 83fb74f44..b306a2612 100644 --- a/rdflib/plugins/sparql/evaluate.py +++ b/rdflib/plugins/sparql/evaluate.py @@ -630,7 +630,7 @@ def evalDescribeQuery(ctx: QueryContext, query) -> Dict[str, Union[str, Graph]]: # Get a CBD for all resources identified to describe for resource in to_describe: # type error: Item "None" of "Optional[Graph]" has no attribute "cbd" - ctx.graph.cbd(resource, graph) # type: ignore[union-attr] + ctx.graph.cbd(resource, target_graph=graph) # type: ignore[union-attr] res: Dict[str, Union[str, Graph]] = {} res["type_"] = "DESCRIBE" diff --git a/test/test_graph/test_graph_cbd.py b/test/test_graph/test_graph_cbd.py index 66861241a..cb9e3761b 100644 --- a/test/test_graph/test_graph_cbd.py +++ b/test/test_graph/test_graph_cbd.py @@ -4,7 +4,8 @@ import pytest from rdflib import Graph, Namespace -from rdflib.term import URIRef +from rdflib.namespace import RDF, RDFS +from rdflib.term import Literal, URIRef EXAMPLE_GRAPH_FILE_PATH = TEST_DATA_DIR / "spec" / "cbd" / "example_graph.rdf" EXAMPLE_GRAPH_CBD_FILE_PATH = TEST_DATA_DIR / "spec" / "cbd" / "example_graph_cbd.rdf" @@ -134,3 +135,27 @@ def test_cbd_example(): assert len(g.cbd(URIRef(query))) == ( 21 ), "cbd() for aReallyGreatBook should return 21 triples" + + +def test_cbd_target(rdfs_graph: Graph): + """ + `Graph.cbd` places the Concise Bounded Description in the target graph. + """ + + target = Graph() + result = rdfs_graph.cbd(RDFS.Literal, target_graph=target) + + expected_result = { + (RDFS.Literal, RDFS.subClassOf, RDFS.Resource), + (RDFS.Literal, RDF.type, RDFS.Class), + (RDFS.Literal, RDFS.label, Literal("Literal")), + ( + RDFS.Literal, + RDFS.comment, + Literal("The class of literal values, eg. textual strings and integers."), + ), + (RDFS.Literal, RDFS.isDefinedBy, URIRef(f"{RDFS}")), + } + + assert result is target + assert expected_result == set(result.triples((None, None, None)))