summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2016-07-16 14:17:16 +0300
committerClaudiu Popa <pcmanticore@gmail.com>2016-07-16 14:17:16 +0300
commitc61b992458e1b1b3d955abf11ac3b75c98a11503 (patch)
tree1d83e532b407f73fb5a9b6bc817554a44ffe4689
parent579ccd51bea9f0e174fa24d8ad5b18032e622e9c (diff)
downloadastroid-git-c61b992458e1b1b3d955abf11ac3b75c98a11503.tar.gz
Make Uninferable have a false value by default.
-rw-r--r--astroid/tests/unittest_inference.py20
-rw-r--r--astroid/util.py5
2 files changed, 18 insertions, 7 deletions
diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py
index 5ca3f05c..70b6f8da 100644
--- a/astroid/tests/unittest_inference.py
+++ b/astroid/tests/unittest_inference.py
@@ -2232,6 +2232,18 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
inferred = next(node.infer())
self.assertEqual(inferred.bool_value(), expected)
+ def test_genexpr_bool_value(self):
+ node = extract_node('''(x for x in range(10))''')
+ self.assertTrue(node.bool_value())
+
+ def test_name_bool_value(self):
+ node = extract_node('''
+ x = 42
+ y = x
+ y
+ ''')
+ self.assertIs(node.bool_value(), util.Uninferable)
+
def test_bool_value(self):
# Verify the truth value of nodes.
module = parse('''
@@ -2240,7 +2252,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
def function(): pass
class Class(object):
def method(self): pass
- genexpr = (x for x in range(10))
dict_comp = {x:y for (x, y) in ((1, 2), (2, 3))}
set_comp = {x for x in range(10)}
list_comp = [x for x in range(10)]
@@ -2253,7 +2264,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
def true_value():
return True
generator = generator_func()
- name = generator
bin_op = 1 + 2
bool_op = x and y
callfunc = test()
@@ -2268,8 +2278,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
self.assertTrue(function.bool_value())
klass = module['Class']
self.assertTrue(klass.bool_value())
- genexpr = next(module['genexpr'].infer())
- self.assertTrue(genexpr.bool_value())
dict_comp = next(module['dict_comp'].infer())
self.assertEqual(dict_comp, util.Uninferable)
set_comp = next(module['set_comp'].infer())
@@ -2284,10 +2292,8 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
self.assertTrue(bound_method)
generator = next(module['generator'].infer())
self.assertTrue(generator)
- name = module['name'].parent.value
- self.assertTrue(name.bool_value())
bin_op = module['bin_op'].parent.value
- self.assertTrue(bin_op.bool_value())
+ self.assertIs(bin_op.bool_value(), util.Uninferable)
bool_op = module['bool_op'].parent.value
self.assertEqual(bool_op.bool_value(), util.Uninferable)
callfunc = module['callfunc'].parent.value
diff --git a/astroid/util.py b/astroid/util.py
index 1111202f..f159fdb4 100644
--- a/astroid/util.py
+++ b/astroid/util.py
@@ -50,6 +50,11 @@ class Uninferable(object):
def __call__(self, *args, **kwargs):
return self
+ def __bool__(self):
+ return False
+
+ __nonzero__ = __bool__
+
def accept(self, visitor):
func = getattr(visitor, "visit_uninferable")
return func(self)