feat: Don't generate prefixes for unknown URIs · RDFLib/rdflib@bd797ac (original) (raw)

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -40,7 +40,8 @@ def preprocess(self) -> None:
40 40 if len(context) == 0:
41 41 continue
42 42 self.store = context
43 -self.getQName(context.identifier)
43 +# Don't generate a new prefix for a graph URI if one already exists
44 +self.getQName(context.identifier, False)
44 45 self._subjects = {}
45 46
46 47 for triple in context:
@@ -97,7 +98,8 @@ def serialize(
97 98 if isinstance(store.identifier, BNode):
98 99 iri = store.identifier.n3()
99 100 else:
100 -iri = self.getQName(store.identifier)
101 +# Show the full graph URI if a prefix for it doesn't already exist
102 +iri = self.getQName(store.identifier, False)
101 103 if iri is None:
102 104 # type error: "IdentifiedNode" has no attribute "n3"
103 105 iri = store.identifier.n3() # type: ignore[attr-defined]
Original file line number Diff line number Diff line change
@@ -60,15 +60,15 @@ def test_remember_namespace():
60 60 # prefix for the graph but later serialize() calls would work.
61 61 first_out = g.serialize(format="trig", encoding="latin-1")
62 62 second_out = g.serialize(format="trig", encoding="latin-1")
63 -assert b"@prefix ns1: http://example.com/ ." in second_out
64 -assert b"@prefix ns1: http://example.com/ ." in first_out
63 +assert b"@prefix ns1: http://example.com/ ." not in second_out
64 +assert b"@prefix ns1: http://example.com/ ." not in first_out
65 65
66 66
67 67 def test_graph_qname_syntax():
68 68 g = rdflib.ConjunctiveGraph()
69 69 g.add(TRIPLE + (rdflib.URIRef("http://example.com/graph1"),))
70 70 out = g.serialize(format="trig", encoding="latin-1")
71 -assert b"ns1:graph1 {" in out
71 +assert b"ns1:graph1 {" not in out
72 72
73 73
74 74 def test_graph_uri_syntax():
@@ -178,9 +178,9 @@ def test_prefixes():
178 178 cg.parse(data=data, format="trig")
179 179 data = cg.serialize(format="trig", encoding="latin-1")
180 180
181 -assert "ns2: <http://ex.org/docs/".encode("latin-1") in data, data
181 +assert "ns2: <http://ex.org/docs/".encode("latin-1") not in data, data
182 182 assert "ns2:document1".encode("latin-1") not in data, data
183 -assert "ns2:document1".encode("latin-1") in data, data
183 +assert "ns2:document1".encode("latin-1") not in data, data
184 184
185 185
186 186 def test_issue_2154():