Skip to content

Bug: Unexpected namespace creation during turtle file serialization #2779

@mickremedi

Description

@mickremedi

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:

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 .

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions