RDF Literal "1"^^xsd:boolean should not coerce to True · Issue #847 · RDFLib/rdflib (original) (raw)

I encountered this issue while working on pySHACL.
Specifically, this bug is causing a failure in one of the tests in the standard data-shapes-test-suite here uniqueLang-002-shapes.ttl.
This test relies on the fact that "1"^^xsd:boolean is an invalid Literal, and when testing equality of this Literal against RDF True, it should be not equal.

A simple code recreation:

import rdflib from rdflib.namespace import XSD fail_bool = rdflib.Literal("1", datatype=XSD.boolean) true_bool = rdflib.Literal("true", datatype=XSD.boolean) print("value: {} , datatype: {} ".format( str(fail_bool._value), str(fail_bool.datatype))) try: assert not (fail_bool == true_bool),
""1" should not equal "true"" except AssertionError as a: print("assertion fail: \n{}".format(str(a)))

This is a more complete example:
https://gist.github.com/ashleysommer/87f0b9660a71de380889f98745af2f74

I've tracked down the problem to this line in the XSDToPython map:

URIRef(_XSD_PFX + 'boolean'): lambda i: i.lower() in ['1', 'true'],

lambda i: i.lower() in ['1', 'true']
should be changed to
lambda i: i.lower() == 'true'