Skip to content

Commit 1634409

Browse files
committed
fix: restore the 6.1.1 default bound namespaces
The namespaces bound by default by `rdflib.graph.Graph` and `rdflib.namespace.NamespaceManager` was reduced in version 6.2.0 of RDFLib, however, this also would cause code that worked with 6.1.1 to break, so this constituted a breaking change. This change restores the previous behavior, binding the same namespaces as was bound in 6.1.1. To bind a reduced set of namespaces the `bind_namespaces` parameter of `rdflib.graph.Graph` or `rdflib.namespace.NamespaceManager` can be used. - Closes <#2103>.
1 parent 4da67f9 commit 1634409

File tree

3 files changed

+104
-9
lines changed

3 files changed

+104
-9
lines changed

rdflib/graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ def __init__(
437437
identifier: Optional[Union[_ContextIdentifierType, str]] = None,
438438
namespace_manager: Optional[NamespaceManager] = None,
439439
base: Optional[str] = None,
440-
bind_namespaces: "_NamespaceSetString" = "core",
440+
bind_namespaces: "_NamespaceSetString" = "rdflib",
441441
):
442442
super(Graph, self).__init__()
443443
self.base = base

rdflib/namespace/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,20 +360,28 @@ class NamespaceManager(object):
360360
* core:
361361
* binds several core RDF prefixes only
362362
* owl, rdf, rdfs, xsd, xml from the NAMESPACE_PREFIXES_CORE object
363-
* this is default
364363
* rdflib:
365364
* binds all the namespaces shipped with RDFLib as DefinedNamespace instances
366365
* all the core namespaces and all the following: brick, csvw, dc, dcat
367366
* dcmitype, dcterms, dcam, doap, foaf, geo, odrl, org, prof, prov, qb, schema
368367
* sh, skos, sosa, ssn, time, vann, void
369368
* see the NAMESPACE_PREFIXES_RDFLIB object for the up-to-date list
369+
* this is default
370370
* none:
371371
* binds no namespaces to prefixes
372372
* note this is NOT default behaviour
373373
* cc:
374374
* using prefix bindings from prefix.cc which is a online prefixes database
375375
* not implemented yet - this is aspirational
376376
377+
.. attention::
378+
379+
The namespaces bound for specific values of ``bind_namespaces``
380+
constitute part of RDFLib's public interface, so changes to them should
381+
only be additive within the same minor version. Removing values, or
382+
removing namespaces that are bound by default, constitutes a breaking
383+
change.
384+
377385
See the
378386
Sample usage
379387
@@ -390,10 +398,11 @@ class NamespaceManager(object):
390398
>>> all_ns = [n for n in g.namespace_manager.namespaces()]
391399
>>> assert ('ex', rdflib.term.URIRef('http://example.com/')) in all_ns
392400
>>>
393-
394401
"""
395402

396-
def __init__(self, graph: "Graph", bind_namespaces: "_NamespaceSetString" = "core"):
403+
def __init__(
404+
self, graph: "Graph", bind_namespaces: "_NamespaceSetString" = "rdflib"
405+
):
397406
self.graph = graph
398407
self.__cache: Dict[str, Tuple[str, URIRef, str]] = {}
399408
self.__cache_strict: Dict[str, Tuple[str, URIRef, str]] = {}

test/test_namespace/test_namespacemanager.py

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,41 @@ def test_core_prefixes_bound():
3333
g = Graph()
3434

3535
# prefixes in Graph
36-
assert len(list(g.namespaces())) == len(_NAMESPACE_PREFIXES_CORE)
36+
assert len(list(g.namespaces())) == len(
37+
{**_NAMESPACE_PREFIXES_RDFLIB, **_NAMESPACE_PREFIXES_CORE}
38+
)
3739
pre = sorted([x[0] for x in list(g.namespaces())])
38-
assert pre == ["owl", "rdf", "rdfs", "xml", "xsd"]
40+
assert pre == [
41+
"brick",
42+
"csvw",
43+
"dc",
44+
"dcam",
45+
"dcat",
46+
"dcmitype",
47+
"dcterms",
48+
"doap",
49+
"foaf",
50+
"geo",
51+
"odrl",
52+
"org",
53+
"owl",
54+
"prof",
55+
"prov",
56+
"qb",
57+
"rdf",
58+
"rdfs",
59+
"schema",
60+
"sh",
61+
"skos",
62+
"sosa",
63+
"ssn",
64+
"time",
65+
"vann",
66+
"void",
67+
"wgs",
68+
"xml",
69+
"xsd",
70+
]
3971

4072

4173
def test_rdflib_prefixes_bound():
@@ -175,6 +207,40 @@ def test_nman_bind_namespaces(
175207
@pytest.mark.parametrize(
176208
["selector", "expected_bindings"],
177209
[
210+
(
211+
None,
212+
{
213+
"brick": "https://brickschema.org/schema/Brick#",
214+
"csvw": "http://www.w3.org/ns/csvw#",
215+
"dc": "http://purl.org/dc/elements/1.1/",
216+
"dcat": "http://www.w3.org/ns/dcat#",
217+
"dcmitype": "http://purl.org/dc/dcmitype/",
218+
"dcterms": "http://purl.org/dc/terms/",
219+
"dcam": "http://purl.org/dc/dcam/",
220+
"doap": "http://usefulinc.com/ns/doap#",
221+
"foaf": "http://xmlns.com/foaf/0.1/",
222+
"odrl": "http://www.w3.org/ns/odrl/2/",
223+
"geo": "http://www.opengis.net/ont/geosparql#",
224+
"org": "http://www.w3.org/ns/org#",
225+
"owl": "http://www.w3.org/2002/07/owl#",
226+
"prof": "http://www.w3.org/ns/dx/prof/",
227+
"prov": "http://www.w3.org/ns/prov#",
228+
"qb": "http://purl.org/linked-data/cube#",
229+
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
230+
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
231+
"schema": "https://schema.org/",
232+
"sh": "http://www.w3.org/ns/shacl#",
233+
"skos": "http://www.w3.org/2004/02/skos/core#",
234+
"sosa": "http://www.w3.org/ns/sosa/",
235+
"ssn": "http://www.w3.org/ns/ssn/",
236+
"time": "http://www.w3.org/2006/time#",
237+
"vann": "http://purl.org/vocab/vann/",
238+
"void": "http://rdfs.org/ns/void#",
239+
"wgs": "https://www.w3.org/2003/01/geo/wgs84_pos#",
240+
"xsd": "http://www.w3.org/2001/XMLSchema#",
241+
"xml": "http://www.w3.org/XML/1998/namespace",
242+
},
243+
),
178244
(
179245
"rdflib",
180246
{
@@ -208,19 +274,39 @@ def test_nman_bind_namespaces(
208274
"xsd": "http://www.w3.org/2001/XMLSchema#",
209275
"xml": "http://www.w3.org/XML/1998/namespace",
210276
},
211-
)
277+
),
278+
(
279+
"core",
280+
{
281+
"owl": "http://www.w3.org/2002/07/owl#",
282+
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
283+
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
284+
"xsd": "http://www.w3.org/2001/XMLSchema#",
285+
"xml": "http://www.w3.org/XML/1998/namespace",
286+
},
287+
),
212288
],
213289
)
214290
def test_bound_namespaces_subset(
215-
selector: Any, expected_bindings: Dict[str, str]
291+
selector: Optional[Any], expected_bindings: Dict[str, str]
216292
) -> None:
217-
graph = Graph(bind_namespaces=selector)
293+
if selector is not None:
294+
graph = Graph(bind_namespaces=selector)
295+
else:
296+
graph = Graph()
218297
bound_namespaces = dict(
219298
(key, str(value)) for key, value in graph.namespace_manager.namespaces()
220299
)
221300
assert (
222301
expected_bindings.items() <= bound_namespaces.items()
223302
), f"missing items {expected_bindings.items() - bound_namespaces.items()}"
303+
empty_graph = Graph(bind_namespaces="none")
304+
if selector is not None:
305+
nman = NamespaceManager(empty_graph, bind_namespaces=selector)
306+
else:
307+
nman = NamespaceManager(empty_graph)
308+
nman_bound_namespaces = dict((key, str(value)) for key, value in nman.namespaces())
309+
assert bound_namespaces == nman_bound_namespaces
224310

225311

226312
def test_compute_qname_no_generate() -> None:

0 commit comments

Comments
 (0)