Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #913 : Added _parseBoolean function to enforce correct Lexical-to-value mapping #995

Merged
merged 8 commits into from
Apr 6, 2020
12 changes: 11 additions & 1 deletion rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,16 @@ def _unhexlify(value):
value = value.encode()
return unhexlify(value)

def _parseBoolean(value):
true_accepted_values = ['1', 'true']
false_accepted_values = ['0', 'false']
new_value = value.lower()
if new_value in true_accepted_values:
return True
if new_value not in false_accepted_values:
warnings.warn('Parsing weird boolean, % r does not map to True or False' % value, category = DeprecationWarning)
return False

# Cannot import Namespace/XSD because of circular dependencies
_XSD_PFX = 'http://www.w3.org/2001/XMLSchema#'
_RDF_PFX = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
Expand Down Expand Up @@ -1552,7 +1562,7 @@ def _castPythonToLiteral(obj, datatype):
URIRef(_XSD_PFX + 'normalizedString'): None,
URIRef(_XSD_PFX + 'token'): None,
URIRef(_XSD_PFX + 'language'): None,
URIRef(_XSD_PFX + 'boolean'): lambda i: i.lower() == 'true',
URIRef(_XSD_PFX + 'boolean'): _parseBoolean,
URIRef(_XSD_PFX + 'decimal'): Decimal,
URIRef(_XSD_PFX + 'integer'): long_type,
URIRef(_XSD_PFX + 'nonPositiveInteger'): int,
Expand Down
25 changes: 24 additions & 1 deletion test/test_literal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

import rdflib # needed for eval(repr(...)) below
from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind
from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind, _XSD_BOOLEAN
from six import integer_types, PY3, string_types


Expand Down Expand Up @@ -100,6 +100,29 @@ def testNoDanglingPoint(self):
out = vv._literal_n3(use_plain=True)
self.assertTrue(out in ["8.8e-01", "0.88"], out)

class TestParseBoolean(unittest.TestCase):
"""confirms the fix for https://github.com/RDFLib/rdflib/issues/913"""
def testTrueBoolean(self):
test_value = Literal("tRue", datatype = _XSD_BOOLEAN)
self.assertTrue(test_value.value)
test_value = Literal("1",datatype = _XSD_BOOLEAN)
self.assertTrue(test_value.value)

def testFalseBoolean(self):
test_value = Literal("falsE", datatype = _XSD_BOOLEAN)
self.assertFalse(test_value.value)
test_value = Literal("0",datatype = _XSD_BOOLEAN)
self.assertFalse(test_value.value)

def testNonFalseBoolean(self):
test_value = Literal("abcd", datatype = _XSD_BOOLEAN)
self.assertRaises(DeprecationWarning)
self.assertFalse(test_value.value)
test_value = Literal("10",datatype = _XSD_BOOLEAN)
self.assertRaises(DeprecationWarning)
self.assertFalse(test_value.value)



class TestBindings(unittest.TestCase):

Expand Down