diff --git a/rdflib/plugins/serializers/trig.py b/rdflib/plugins/serializers/trig.py
index a151827ca..b30d10c8b 100644
--- a/rdflib/plugins/serializers/trig.py
+++ b/rdflib/plugins/serializers/trig.py
@@ -68,7 +68,10 @@ def serialize(self, stream, base=None, encoding=None,
if self.default_context and store.identifier==self.default_context:
self.write(self.indent() + '\n{')
else:
- self.write(self.indent() + '\n<%s> {' % self.getQName(store.identifier))
+ iri = self.getQName(store.identifier)
+ if iri is None:
+ iri = '<%s>' % store.identifier
+ self.write(self.indent() + '\n%s {' % iri)
self.depth += 1
for subject in ordered_subjects:
diff --git a/test/test_trig.py b/test/test_trig.py
index 0f800af34..862bff12a 100644
--- a/test/test_trig.py
+++ b/test/test_trig.py
@@ -61,3 +61,23 @@ def testRememberNamespace(self):
self.assertIn('@prefix ns1: .', first_out)
print first_out
+
+ def testGraphQnameSyntax(self):
+ g = rdflib.ConjunctiveGraph()
+ g.add((rdflib.URIRef("http://example.com/s"),
+ rdflib.RDFS.label,
+ rdflib.Literal("example 1"),
+ rdflib.URIRef("http://example.com/graph1")))
+ out = g.serialize(format='trig')
+ self.assertIn('ns1:graph1 {', out)
+
+ def testGraphUriSyntax(self):
+ g = rdflib.ConjunctiveGraph()
+ g.add((rdflib.URIRef("http://example.com/s"),
+ rdflib.RDFS.label,
+ rdflib.Literal("example 1"),
+ # getQName will not abbreviate this, so it should come
+ # out as a '<...>' term.
+ rdflib.URIRef("http://example.com/foo.")))
+ out = g.serialize(format='trig')
+ self.assertIn(' {', out)