summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2015-11-29 22:11:22 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2015-11-29 22:11:22 +0200
commitc486ba77ea44edc119e723286e26158a65436037 (patch)
tree508699456ce844e2707ccd15a850ac3c04f2520e
parent65cf5619390921d2b08fcd1899b54197bafeefb8 (diff)
downloadastroid-c486ba77ea44edc119e723286e26158a65436037.tar.gz
Add support for handling Uninferable nodes when calling as_string
Some object, for instance List or Tuple can have, after inference, Uninferable as their elements, happening when their components weren't couldn't be inferred properly. This means that as_string needs to cope with expecting Uninferable nodes part of the other nodes coming for a string transformation. The patch adds a visit method in AsString and ``accept`` on Yes / Uninferable nodes. Closes issue #270.
-rw-r--r--astroid/as_string.py3
-rw-r--r--astroid/tests/unittest_nodes.py10
-rw-r--r--astroid/util.py6
3 files changed, 19 insertions, 0 deletions
diff --git a/astroid/as_string.py b/astroid/as_string.py
index 0d7ba51..b2b2c95 100644
--- a/astroid/as_string.py
+++ b/astroid/as_string.py
@@ -435,6 +435,9 @@ class AsStringVisitor(object):
def visit_super(self, node):
return node.parent.accept(self)
+ def visit_uninferable(self, node):
+ return str(node)
+
class AsStringVisitor3(AsStringVisitor):
"""AsStringVisitor3 overwrites some AsStringVisitor methods"""
diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py
index 3677bfb..e2e8338 100644
--- a/astroid/tests/unittest_nodes.py
+++ b/astroid/tests/unittest_nodes.py
@@ -62,6 +62,16 @@ class AsStringTest(resources.SysPathSetup, unittest.TestCase):
node = parse(code)
self.assertEqual(node.as_string().strip(), code.strip())
+ def test_as_string_for_list_containing_uninferable(self):
+ node = test_utils.extract_node('''
+ def foo():
+ bar = [arg] * 1
+ ''')
+ binop = node.body[0].value
+ inferred = next(binop.infer())
+ self.assertEqual(inferred.as_string(), '[Uninferable]')
+ self.assertEqual(binop.as_string(), '([arg]) * (1)')
+
def test_frozenset_as_string(self):
nodes = test_utils.extract_node('''
frozenset((1, 2, 3)) #@
diff --git a/astroid/util.py b/astroid/util.py
index 1d50f7b..40c0fb2 100644
--- a/astroid/util.py
+++ b/astroid/util.py
@@ -37,17 +37,23 @@ class Uninferable(object):
"""Special inference object, which is returned when inference fails."""
def __repr__(self):
return 'Uninferable'
+ __str__ = __repr__
def __getattribute__(self, name):
if name == 'next':
raise AttributeError('next method should not be called')
if name.startswith('__') and name.endswith('__'):
return object.__getattribute__(self, name)
+ if name == 'accept':
+ return object.__getattribute__(self, name)
return self
def __call__(self, *args, **kwargs):
return self
+ def accept(self, visitor):
+ func = getattr(visitor, "visit_uninferable")
+ return func(self)
class BadOperationMessage(object):
"""Object which describes a TypeError occurred somewhere in the inference chain