summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Pribysh <dmand@yandex.ru>2015-10-24 00:45:36 +0300
committerDmitry Pribysh <dmand@yandex.ru>2015-10-24 00:45:36 +0300
commit63d38d6b6d41d2259f909faa51a8272220dd2de7 (patch)
tree7251aa89b9625c12b7cf0f7e99e3654c19549253
parentb50e93e14255cb63a60623e214384ecf78dcc483 (diff)
downloadastroid-63d38d6b6d41d2259f909faa51a8272220dd2de7.tar.gz
Fix inference of list multiplication operation.
Old implementation was pushing all the possible values inferred from a node into a list, thus creating extra items in case when there are multiple infered values yielded. New implementation takes only first infered value, preserving the expected size of the list. Fixes pylint's #559.
-rw-r--r--astroid/protocols.py9
-rw-r--r--astroid/tests/unittest_inference.py13
2 files changed, 19 insertions, 3 deletions
diff --git a/astroid/protocols.py b/astroid/protocols.py
index 23a4f2e..c51464d 100644
--- a/astroid/protocols.py
+++ b/astroid/protocols.py
@@ -141,9 +141,12 @@ nodes.Const.infer_binary_op = const_infer_binary_op
def _multiply_seq_by_int(self, other, context):
node = self.__class__()
- elts = [n for elt in self.elts for n in elt.infer(context)
- if not n is util.YES] * other.value
- node.elts = elts
+ elts = []
+ for elt in self.elts:
+ infered = next(elt.infer(context))
+ if not infered is util.YES:
+ elts.append(infered)
+ node.elts = elts * other.value
return node
diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py
index bce1db4..6adaada 100644
--- a/astroid/tests/unittest_inference.py
+++ b/astroid/tests/unittest_inference.py
@@ -1018,6 +1018,19 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
self.assertEqual(len(inferred), 1)
self.assertEqual(inferred[0], util.YES)
+ def test_binary_op_list_mul_int(self):
+ 'test correct handling on list multiplied by int when there are more than one'
+ code = '''
+ from ctypes import c_int
+ seq = [c_int()] * 4
+ '''
+ ast = parse(code, __name__)
+ inferred = ast['seq'].inferred()
+ self.assertEqual(len(inferred), 1)
+ listval = inferred[0]
+ self.assertIsInstance(listval, nodes.List)
+ self.assertEqual(len(listval.itered()), 4)
+
def test_binary_op_on_self(self):
'test correct handling of applying binary operator to self'
code = '''