Implicit conversion to boolean in PYSON statement

issue7528
review58271002
This commit is contained in:
Cédric Krier
2018-07-10 16:55:37 +02:00
parent d9ff752284
commit 9681897041
3 changed files with 19 additions and 27 deletions
+1
View File
@@ -1,3 +1,4 @@
* Implicit conversion to boolean in PYSON statement
* Add domain to dictionary schema
* Fill main language in ir.configuration at database initialization
* Remove support for Python 2.7
+18 -16
View File
@@ -161,9 +161,10 @@ class Not(PYSON):
def __init__(self, v):
super(Not, self).__init__()
if isinstance(v, PYSON):
assert v.types() == {bool}, 'value must be boolean'
else:
assert isinstance(v, bool), 'value must be boolean'
if v.types() != {bool}:
v = Bool(v)
elif not isinstance(v, bool):
v = bool(v)
self._value = v
@property
@@ -213,13 +214,12 @@ class And(PYSON):
def __init__(self, *statements, **kwargs):
super(And, self).__init__()
statements = list(statements) + kwargs.get('s', [])
for statement in statements:
for i, statement in enumerate(list(statements)):
if isinstance(statement, PYSON):
assert statement.types() == {bool}, \
'statement must be boolean'
else:
assert isinstance(statement, bool), \
'statement must be boolean'
if statement.types() != {bool}:
statements[i] = Bool(statement)
elif not isinstance(statement, bool):
statements[i] = bool(statement)
assert len(statements) >= 2, 'must have at least 2 statements'
self._statements = statements
@@ -302,9 +302,10 @@ class Greater(PYSON):
assert isinstance(i, (int, float, type(None))), \
'statement must be an integer or a float'
if isinstance(equal, PYSON):
assert equal.types() == {bool}
else:
assert isinstance(equal, bool)
if equal.types() != {bool}:
equal = Bool(equal)
elif not isinstance(equal, bool):
equal = bool(equal)
self._statement1 = statement1
self._statement2 = statement2
self._equal = equal
@@ -365,10 +366,10 @@ class If(PYSON):
condition, then_statement, else_statement = c, t, e
super(If, self).__init__()
if isinstance(condition, PYSON):
assert condition.types() == {bool}, \
'condition must be boolean'
else:
assert isinstance(condition, bool), 'condition must be boolean'
if condition.types() != {bool}:
condition = Bool(condition)
elif not isinstance(condition, bool):
condition = bool(condition)
if isinstance(then_statement, PYSON):
then_types = then_statement.types()
else:
@@ -678,6 +679,7 @@ class Id(PYSON):
return {int}
CONTEXT = {
'Eval': Eval,
'Not': Not,
-11
View File
@@ -51,8 +51,6 @@ class PYSONTestCase(unittest.TestCase):
'v': True,
})
self.assertRaises(AssertionError, pyson.Not, 'foo')
self.assertEqual(pyson.Not(True).types(), set([bool]))
eval = pyson.PYSONEncoder().encode(pyson.Not(True))
@@ -111,9 +109,6 @@ class PYSONTestCase(unittest.TestCase):
's': [True, False],
})
self.assertRaises(AssertionError, pyson.And, 'test', False)
self.assertRaises(AssertionError, pyson.And, True, 'test')
self.assertRaises(AssertionError, pyson.And, True, False, 'test')
self.assertRaises(AssertionError, pyson.And, True)
self.assertRaises(AssertionError, pyson.And)
@@ -156,9 +151,6 @@ class PYSONTestCase(unittest.TestCase):
's': [True, False],
})
self.assertRaises(AssertionError, pyson.Or, 'test', False)
self.assertRaises(AssertionError, pyson.Or, True, 'test')
self.assertRaises(AssertionError, pyson.Or, True, False, 'test')
self.assertRaises(AssertionError, pyson.Or, True)
self.assertRaises(AssertionError, pyson.Or)
@@ -226,7 +218,6 @@ class PYSONTestCase(unittest.TestCase):
self.assertRaises(AssertionError, pyson.Greater, 'test', 0)
self.assertRaises(AssertionError, pyson.Greater, 1, 'test')
self.assertRaises(AssertionError, pyson.Greater, 1, 0, 'test')
self.assertEqual(pyson.Greater(1, 0).types(), set([bool]))
@@ -267,7 +258,6 @@ class PYSONTestCase(unittest.TestCase):
self.assertRaises(AssertionError, pyson.Less, 'test', 1)
self.assertRaises(AssertionError, pyson.Less, 0, 'test')
self.assertRaises(AssertionError, pyson.Less, 0, 1, 'test')
self.assertEqual(pyson.Less(0, 1).types(), set([bool]))
@@ -306,7 +296,6 @@ class PYSONTestCase(unittest.TestCase):
'e': 'bar',
})
self.assertRaises(AssertionError, pyson.If, 'test', 'foo', 'bar')
self.assertRaises(AssertionError, pyson.If, True, 'foo', False)
self.assertEqual(pyson.If(True, 'foo', 'bar').types(),