diff options
| -rw-r--r-- | scoped_nodes.py | 15 | ||||
| -rw-r--r-- | test/unittest_scoped_nodes.py | 5 |
2 files changed, 13 insertions, 7 deletions
diff --git a/scoped_nodes.py b/scoped_nodes.py index 3a856992..145868d2 100644 --- a/scoped_nodes.py +++ b/scoped_nodes.py @@ -351,11 +351,12 @@ class FunctionNG(object): def argnames(self): """return argument names if there are any arguments""" - if not self.args.args: # can be None for builtins - return [] - else: - #return [arg.name for arg in self.args.args] - return _get_names(self.args.args) + names = _rec_get_names(self.args.args) + if self.args.vararg: + names.append(self.args.vararg) + if self.args.kwarg: + names.append(self.args.kwarg) + return names def is_bound(self): """return true if the function is bound to an Instance or a class""" @@ -399,13 +400,13 @@ Lambda.argnames = FunctionNG.argnames.im_func # XXX need to cleanup these args / arguments methods -def _get_names(args, names=None): +def _rec_get_names(args, names=None): """return a list of all argument names""" if names is None: names = [] for arg in args: if isinstance(arg, Tuple): - _get_names(arg.elts, names) + _rec_get_names(arg.elts, names) else: names.append(arg.name) return names diff --git a/test/unittest_scoped_nodes.py b/test/unittest_scoped_nodes.py index 00654327..0529e0e9 100644 --- a/test/unittest_scoped_nodes.py +++ b/test/unittest_scoped_nodes.py @@ -181,6 +181,11 @@ def f(): g = list(astng['f'].ilookup('g'))[0] self.failUnlessEqual(g.pytype(), '__builtin__.function') + def test_argnames(self): + code = 'def f(a, (b, c), *args, **kwargs): pass' + astng = abuilder.string_build(code, __name__, __file__) + self.assertEquals(astng['f'].argnames(), ['a', 'b', 'c', 'args', 'kwargs']) + class ClassNodeTC(TestCase): |
