Skip to content

Commit 17cffe2

Browse files
author
Graham Higgins
committed
Raise exception if prefix not bound, expand exception message, add test of objects/messages.
1 parent 4a67390 commit 17cffe2

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

rdflib/namespace/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,11 @@ def expand_curie(self, curie: str) -> Union[URIRef, None]:
600600
>>> g.namespace_manager.expand_curie("rdf:type")
601601
rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')
602602
603-
Returns `None` if a namespace is not bound to the prefix.
603+
Raises exception if a namespace is not bound to the prefix.
604+
604605
"""
605606
if not type(curie) is str:
606-
raise TypeError("Argument must be a string.")
607+
raise TypeError(f"Argument must be a string, not {type(curie).__name__}.")
607608
parts = curie.split(":", 1)
608609
if len(parts) != 2 or len(parts[0]) < 1:
609610
raise ValueError(
@@ -612,7 +613,10 @@ def expand_curie(self, curie: str) -> Union[URIRef, None]:
612613
ns = self.store.namespace(parts[0])
613614
if ns is not None:
614615
return URIRef(f"{str(ns)}{parts[1]}")
615-
return None
616+
else:
617+
raise ValueError(
618+
f"Prefix \"{curie.split(':')[0]}\" not bound to any namespace."
619+
)
616620

617621
def bind(
618622
self,

test/test_namespace/test_namespace.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99

1010
from rdflib import DCTERMS
11-
from rdflib.graph import Graph
11+
from rdflib.graph import BNode, Graph, Literal
1212
from rdflib.namespace import (
1313
FOAF,
1414
OWL,
@@ -264,6 +264,40 @@ def test_contains_method(self):
264264
ref = URIRef("http://www.w3.org/2002/07/owl#real")
265265
assert ref in OWL, "OWL does not include owl:real"
266266

267+
def test_expand_curie(self) -> None:
268+
g = Graph()
269+
270+
assert g.namespace_manager.expand_curie("rdf:type") == URIRef(
271+
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
272+
)
273+
274+
assert g.namespace_manager.expand_curie("rdf:type") == RDF.type
275+
276+
g.bind("ex", Namespace("urn:example:"))
277+
278+
assert g.namespace_manager.expand_curie("ex:tarek") == URIRef(
279+
"urn:example:tarek"
280+
)
281+
282+
def test_expand_curie_exception_messages(self) -> None:
283+
g = Graph()
284+
285+
with pytest.raises(TypeError) as e:
286+
assert g.namespace_manager.expand_curie(URIRef("urn:example")) == None
287+
assert str(e.value) == "Argument must be a string, not URIRef."
288+
289+
with pytest.raises(TypeError) as e:
290+
assert g.namespace_manager.expand_curie(Literal("rdf:type")) == None
291+
assert str(e.value) == "Argument must be a string, not Literal."
292+
293+
with pytest.raises(TypeError) as e:
294+
assert g.namespace_manager.expand_curie(BNode()) == None
295+
assert str(e.value) == "Argument must be a string, not BNode."
296+
297+
with pytest.raises(TypeError) as e:
298+
assert g.namespace_manager.expand_curie(Graph()) == None
299+
assert str(e.value) == "Argument must be a string, not Graph."
300+
267301
@pytest.mark.parametrize(
268302
["curie", "expected_result"],
269303
[
@@ -273,18 +307,20 @@ def test_contains_method(self):
273307
("ex:a:b", URIRef(f"urn:example:a:b")),
274308
("ex:a:b:c", URIRef(f"urn:example:a:b:c")),
275309
("ex", ValueError),
276-
("em:tarek", None),
277-
("em:", None),
310+
("em:tarek", ValueError),
311+
("em:", ValueError),
278312
("em", ValueError),
279313
(":", ValueError),
280314
(":type", ValueError),
281315
("í", ValueError),
282-
(" :", None),
316+
(" :", ValueError),
283317
("", ValueError),
284318
("\n", ValueError),
285319
(None, TypeError),
286320
(3, TypeError),
287321
(URIRef("urn:example:"), TypeError),
322+
(BNode(), TypeError),
323+
(Literal("rdf:type"), TypeError),
288324
],
289325
)
290326
def test_expand_curie(

0 commit comments

Comments
 (0)