-
Notifications
You must be signed in to change notification settings - Fork 581
Open
Description
Hello! I've noticed that serializing a ttl file has an unexpected behavior where adding a triple to a blank graph and then serializing it randomly adds a prefix to the turtle file:
import rdflib
TRIPLE = (
rdflib.URIRef("http://example1.com/s"),
rdflib.URIRef("http://example2.com/p"),
rdflib.Literal("some literal"),
)
g = rdflib.Graph(bind_namespaces="none")
g.add(TRIPLE)
print("Namespaces Before:", list(g.namespaces()))
x = g.serialize(format="turtle")
print(x)
print("Namespaces After:", list(g.namespaces()))Results in:
Namespaces Before: []
@prefix ns1: <http://example2.com/> .
<http://example1.com/s> ns1:p "some literal" .
Namespaces After: [('ns1', rdflib.term.URIRef('http://example2.com/'))]
When someone would expect:
Namespaces Before: []
<http://example1.com/s> <http://example2.com/p> "some literal" .
Namespaces After: []
I've boiled it down to the following line:
rdflib/rdflib/plugins/serializers/turtle.py
Line 270 in fb43b7a
| self.getQName(node, gen_prefix=(i == VERB)) |
Here we create a new prefix if we're looking at the predicate of a triple during serialization. I can't follow the blame of this change or docs explaining that serialize modifies the graph. Does anyone know why this was put there and if it can be set to self.getQName(node, gen_prefix=False)? This seems to have already been done for trig files #2467 .
sardormajano and seo-changseo-changseo-chang
Metadata
Metadata
Assignees
Labels
No labels