Skip to content

Commit d5774ef

Browse files
authored
chore: add deprecation notice to Dataset methods and attributes (#3264)
* chore: add deprecation warning to Dataset.contexts() * chore: deprecate Dataset.default_context and introduce Dataset.default_graph * chore: deprecate Dataset.identifier * chore: replace usage of default_context with default_graph in Dataset
1 parent 0f3237d commit d5774ef

File tree

2 files changed

+104
-11
lines changed

2 files changed

+104
-11
lines changed

rdflib/graph.py

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,7 @@ class ConjunctiveGraph(Graph):
19691969
All queries are carried out against the union of all graphs.
19701970
"""
19711971

1972-
default_context: _ContextType
1972+
_default_context: _ContextType
19731973

19741974
def __init__(
19751975
self,
@@ -1991,10 +1991,18 @@ def __init__(
19911991
)
19921992
self.context_aware = True
19931993
self.default_union = True # Conjunctive!
1994-
self.default_context: _ContextType = Graph(
1994+
self._default_context: _ContextType = Graph(
19951995
store=self.store, identifier=identifier or BNode(), base=default_graph_base
19961996
)
19971997

1998+
@property
1999+
def default_context(self):
2000+
return self._default_context
2001+
2002+
@default_context.setter
2003+
def default_context(self, value):
2004+
self._default_context = value
2005+
19982006
def __str__(self) -> str:
19992007
pattern = (
20002008
"[a rdflib:ConjunctiveGraph;rdflib:storage "
@@ -2519,14 +2527,49 @@ def __init__(
25192527

25202528
if not self.store.graph_aware:
25212529
raise Exception("DataSet must be backed by a graph-aware store!")
2522-
self.default_context = Graph(
2530+
self._default_context = Graph(
25232531
store=self.store,
25242532
identifier=DATASET_DEFAULT_GRAPH_ID,
25252533
base=default_graph_base,
25262534
)
25272535

25282536
self.default_union = default_union
25292537

2538+
@property
2539+
def default_context(self):
2540+
warnings.warn(
2541+
"Dataset.default_context is deprecated, use Dataset.default_graph instead.",
2542+
DeprecationWarning,
2543+
stacklevel=2,
2544+
)
2545+
return self._default_context
2546+
2547+
@default_context.setter
2548+
def default_context(self, value):
2549+
warnings.warn(
2550+
"Dataset.default_context is deprecated, use Dataset.default_graph instead.",
2551+
DeprecationWarning,
2552+
stacklevel=2,
2553+
)
2554+
self._default_context = value
2555+
2556+
@property
2557+
def default_graph(self):
2558+
return self._default_context
2559+
2560+
@default_graph.setter
2561+
def default_graph(self, value):
2562+
self._default_context = value
2563+
2564+
@property
2565+
def identifier(self):
2566+
warnings.warn(
2567+
"Dataset.identifier is deprecated and will be removed in future versions.",
2568+
DeprecationWarning,
2569+
stacklevel=2,
2570+
)
2571+
return super(Dataset, self).identifier
2572+
25302573
def __str__(self) -> str:
25312574
pattern = (
25322575
"[a rdflib:Dataset;rdflib:storage " "[a rdflib:Store;rdfs:label '%s']]"
@@ -2539,14 +2582,14 @@ def __reduce__(self) -> Tuple[Type[Dataset], Tuple[Store, bool]]: # type: ignor
25392582
return (type(self), (self.store, self.default_union))
25402583

25412584
def __getstate__(self) -> Tuple[Store, _ContextIdentifierType, _ContextType, bool]:
2542-
return self.store, self.identifier, self.default_context, self.default_union
2585+
return self.store, self.identifier, self.default_graph, self.default_union
25432586

25442587
def __setstate__(
25452588
self, state: Tuple[Store, _ContextIdentifierType, _ContextType, bool]
25462589
) -> None:
25472590
# type error: Property "store" defined in "Graph" is read-only
25482591
# type error: Property "identifier" defined in "Graph" is read-only
2549-
self.store, self.identifier, self.default_context, self.default_union = state # type: ignore[misc]
2592+
self.store, self.identifier, self.default_graph, self.default_union = state # type: ignore[misc]
25502593

25512594
def graph(
25522595
self,
@@ -2590,7 +2633,7 @@ def parse(
25902633
25912634
If the source is in a format that does not support named graphs its triples
25922635
will be added to the default graph
2593-
(i.e. :attr:`.Dataset.default_context`).
2636+
(i.e. :attr:`.Dataset.default_graph`).
25942637
25952638
.. caution::
25962639
@@ -2611,7 +2654,7 @@ def parse(
26112654
the ``publicID`` parameter will also not be used as the name for the
26122655
graph that the data is loaded into, and instead the triples from sources
26132656
that do not support named graphs will be loaded into the default graph
2614-
(i.e. :attr:`.Dataset.default_context`).
2657+
(i.e. :attr:`.Dataset.default_graph`).
26152658
"""
26162659

26172660
ConjunctiveGraph.parse(
@@ -2632,31 +2675,44 @@ def remove_graph(
26322675
g = self.get_context(g)
26332676

26342677
self.store.remove_graph(g)
2635-
if g is None or g == self.default_context:
2678+
if g is None or g == self.default_graph:
26362679
# default graph cannot be removed
26372680
# only triples deleted, so add it back in
2638-
self.store.add_graph(self.default_context)
2681+
self.store.add_graph(self.default_graph)
26392682
return self
26402683

26412684
def contexts(
26422685
self, triple: Optional[_TripleType] = None
26432686
) -> Generator[_ContextType, None, None]:
2687+
warnings.warn(
2688+
"Dataset.contexts is deprecated, use Dataset.graphs instead.",
2689+
DeprecationWarning,
2690+
stacklevel=2,
2691+
)
26442692
default = False
26452693
for c in super(Dataset, self).contexts(triple):
26462694
default |= c.identifier == DATASET_DEFAULT_GRAPH_ID
26472695
yield c
26482696
if not default:
26492697
yield self.graph(DATASET_DEFAULT_GRAPH_ID)
26502698

2651-
graphs = contexts
2699+
def graphs(
2700+
self, triple: Optional[_TripleType] = None
2701+
) -> Generator[_ContextType, None, None]:
2702+
default = False
2703+
for c in super(Dataset, self).contexts(triple):
2704+
default |= c.identifier == DATASET_DEFAULT_GRAPH_ID
2705+
yield c
2706+
if not default:
2707+
yield self.graph(DATASET_DEFAULT_GRAPH_ID)
26522708

26532709
# type error: Return type "Generator[Tuple[Node, Node, Node, Optional[Node]], None, None]" of "quads" incompatible with return type "Generator[Tuple[Node, Node, Node, Optional[Graph]], None, None]" in supertype "ConjunctiveGraph"
26542710
def quads( # type: ignore[override]
26552711
self, quad: Optional[_TripleOrQuadPatternType] = None
26562712
) -> Generator[_OptionalIdentifiedQuadType, None, None]:
26572713
for s, p, o, c in super(Dataset, self).quads(quad):
26582714
# type error: Item "None" of "Optional[Graph]" has no attribute "identifier"
2659-
if c.identifier == self.default_context: # type: ignore[union-attr]
2715+
if c.identifier == self.default_graph: # type: ignore[union-attr]
26602716
yield s, p, o, None
26612717
else:
26622718
# type error: Item "None" of "Optional[Graph]" has no attribute "identifier" [union-attr]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pytest
2+
3+
from rdflib import Dataset
4+
5+
6+
def test_dataset_contexts_method():
7+
ds = Dataset()
8+
with pytest.warns(
9+
DeprecationWarning,
10+
match="Dataset.contexts is deprecated, use Dataset.graphs instead.",
11+
):
12+
# Call list() to consume the generator to emit the warning.
13+
list(ds.contexts())
14+
15+
16+
def test_dataset_default_context_property():
17+
ds = Dataset()
18+
with pytest.warns(
19+
DeprecationWarning,
20+
match="Dataset.default_context is deprecated, use Dataset.default_graph instead.",
21+
):
22+
ds.default_context
23+
24+
with pytest.warns(
25+
DeprecationWarning,
26+
match="Dataset.default_context is deprecated, use Dataset.default_graph instead.",
27+
):
28+
ds.default_context = ds.graph()
29+
30+
31+
def test_dataset_identifier_property():
32+
ds = Dataset()
33+
with pytest.warns(
34+
DeprecationWarning,
35+
match="Dataset.identifier is deprecated and will be removed in future versions.",
36+
):
37+
ds.identifier

0 commit comments

Comments
 (0)