RDFLib/rdflib — Issue #76: Literal init with both language and datatype
Author: gjhiggins
Created: 2012-02-20 08:32:06+00:00
State: open

kyleheus...@gmail.com, 2009-08-27T22:53:49.000Z

When you initialize a Literal type with both a language and datatype, only
datatype is set (language value is discarded and set to None)

But if you manually set a literal object's language attribute, it sticks
regardless of whether datatype was set.

When you serialize or call the literal's n3() method, it is possible to
output both language and datatype.

So, the question is, why can't a Literal be initialized with both langauge
and datatype (ie. via the __new__ method)?

ex.1
>> l = Literal(u'hello',u'en-us',XSD_NS['string'])
>> l.language
None
>> l.n3()
u'"hello"^^'

ex.2
>> l = Literal(u'hello',XSD_NS['string'])
>> l.language = u'en-us'
>> l.n3()
u'"hello"@en-us^^'

Comment 1 by lindstr...@gmail.com
In RDF literal values cannot have both a language and a datatype. It's either or or neither. Thus this should
reasonably not be changed.

(It *is* possible right now to get those strange serializations by manipulating a Literal instance though, but that
may/should go away as well..)

I propose WontFix.

Comment 2 by eik...@gmail.com

Comment 3 by eik...@gmail.com

Comment 4 by drewpca
I propose, and I hope to write the patch tonight, that Literal raise a TypeError if
both the args are passed in. Quietly ignoring one of the args isn't useful. Also,
__new__ can have a docstring explaining this with a pointer to the spec, since clearly
it's surprising people.

In a related fix, Literal's __repr__ shouldn't ever emit datatype and language args,
and it should not omit None args at all, just to save space.

Comment 5 by drewpca

Comment 6 by drewpca
Waiting for review: http://code.google.com/p/rdflib/source/branch?
spec=issue107&branch=/branches/literalnew-81

Comment 7 by drewpca
Merged, with a change to the rdf/xml parser, since an rdf/xml node CAN have a lang and
datatype. (The lang is ignored, says http://www.w3.org/TR/REC-rdf-syntax/#section-
Syntax-languages)