summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcpopa <devnull@localhost>2014-07-29 11:36:15 +0300
committercpopa <devnull@localhost>2014-07-29 11:36:15 +0300
commit492a0afe6815a8d886e7f0ce0043576a7f3e8f48 (patch)
tree7d605ec8458d8d79685107449972151c8cda110d
parentcd03823a30cce64dd4298c349b6a132a1d945a2a (diff)
downloadastroid-492a0afe6815a8d886e7f0ce0043576a7f3e8f48.tar.gz
Set the parent of vararg and kwarg nodes when inferring them. Closes issue #43.
-rw-r--r--ChangeLog3
-rw-r--r--protocols.py8
-rw-r--r--test/unittest_inference.py20
3 files changed, 29 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index dcc1093..2fff5b8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,9 @@ Change log for the astroid package (used to be astng)
* Fix a crash occurred when inferring decorator call chain.
Closes issue #42.
+ * Set the parent of vararg and kwarg nodes when inferring them.
+ Closes issue #43.
+
2014-07-25 -- 1.2.0
* Function nodes can detect decorator call chain and see if they are
diff --git a/protocols.py b/protocols.py
index 7ce1faa..d621ffb 100644
--- a/protocols.py
+++ b/protocols.py
@@ -231,10 +231,14 @@ def _arguments_infer_argname(self, name, context):
yield self.parent.parent.frame()
return
if name == self.vararg:
- yield const_factory(())
+ vararg = const_factory(())
+ vararg.parent = self
+ yield vararg
return
if name == self.kwarg:
- yield const_factory({})
+ kwarg = const_factory({})
+ kwarg.parent = self
+ yield kwarg
return
# if there is a default value, yield it. And then yield YES to reflect
# we can't guess given argument value
diff --git a/test/unittest_inference.py b/test/unittest_inference.py
index c703681..7e70ed3 100644
--- a/test/unittest_inference.py
+++ b/test/unittest_inference.py
@@ -1220,5 +1220,25 @@ empty_list = A().empty_method()
empty_list = astroid['empty_list'].infered()[0]
self.assertIsInstance(empty_list, nodes.List)
+ def test_infer_variable_arguments(self):
+ code = '''
+def test(*args, **kwargs):
+ vararg = args
+ kwarg = kwargs
+ '''
+ astroid = builder.string_build(code, __name__, __file__)
+ func = astroid['test']
+ vararg = func.body[0].value
+ kwarg = func.body[1].value
+
+ kwarg_infered = kwarg.infered()[0]
+ self.assertIsInstance(kwarg_infered, nodes.Dict)
+ self.assertIs(kwarg_infered.parent, func.args)
+
+ vararg_infered = vararg.infered()[0]
+ self.assertIsInstance(vararg_infered, nodes.Tuple)
+ self.assertIs(vararg_infered.parent, func.args)
+
+
if __name__ == '__main__':
unittest_main()