summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcpopa <devnull@localhost>2014-04-13 11:50:59 +0300
committercpopa <devnull@localhost>2014-04-13 11:50:59 +0300
commit2cd1d6c2cc1d1877474f42bce96dd12e67716b30 (patch)
tree9b6a3bce64b0e6e5cf1a4e0caef500cb4797d3ec
parentf074a945d4c20e5609a54f3e241a7d6bb415a136 (diff)
downloadastroid-2cd1d6c2cc1d1877474f42bce96dd12e67716b30.tar.gz
Fix argument search for methods and classmethods.
-rw-r--r--inference.py5
-rw-r--r--test/unittest_inference.py36
2 files changed, 41 insertions, 0 deletions
diff --git a/inference.py b/inference.py
index 29c97be..35cce33 100644
--- a/inference.py
+++ b/inference.py
@@ -72,6 +72,11 @@ class CallContext:
return iter((boundnode,))
if funcnode.type == 'classmethod':
return iter((boundnode,))
+ # if we have a method, extract one position
+ # from the index, so we'll take in account
+ # the extra parameter represented by `self` or `cls`
+ if funcnode.type in ('method', 'classmethod'):
+ argindex -= 1
# 2. search arg index
try:
return self.args[argindex].infer(context)
diff --git a/test/unittest_inference.py b/test/unittest_inference.py
index ce02f99..c417e1a 100644
--- a/test/unittest_inference.py
+++ b/test/unittest_inference.py
@@ -1183,6 +1183,42 @@ B = namedtuple('B', 'a b')
self.assertIn('a', bclass.instance_attrs)
self.assertIn('b', bclass.instance_attrs)
+ def test_infer_arguments(self):
+ code = '''
+class A(object):
+ def first(self, arg1, arg2):
+ return arg1
+ @classmethod
+ def method(cls, arg1, arg2):
+ return arg2
+ @classmethod
+ def empty(cls):
+ return 2
+ @staticmethod
+ def static(arg1, arg2):
+ return arg1
+ def empty_method(self):
+ return []
+x = A().first(1, [])
+y = A.method(1, [])
+z = A.static(1, [])
+empty = A.empty()
+empty_list = A().empty_method()
+ '''
+ astroid = builder.string_build(code, __name__, __file__)
+ int_node = astroid['x'].infered()[0]
+ self.assertIsInstance(int_node, nodes.Const)
+ self.assertEqual(int_node.value, 1)
+ list_node = astroid['y'].infered()[0]
+ self.assertIsInstance(list_node, nodes.List)
+ int_node = astroid['z'].infered()[0]
+ self.assertIsInstance(int_node, nodes.Const)
+ self.assertEqual(int_node.value, 1)
+ empty = astroid['empty'].infered()[0]
+ self.assertIsInstance(empty, nodes.Const)
+ self.assertEqual(empty.value, 2)
+ empty_list = astroid['empty_list'].infered()[0]
+ self.assertIsInstance(empty_list, nodes.List)
if __name__ == '__main__':
unittest_main()