Skip to content

Commit

Permalink
Optimize argnames() (#2215)
Browse files Browse the repository at this point in the history
Arguments cannot be parenthesized in Python 3.

The unit test for this was already removed in
8ae94aa.
  • Loading branch information
jacobtylerwalls committed Jun 19, 2023
1 parent eabc643 commit 3db2bdd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
16 changes: 2 additions & 14 deletions astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ def argnames(self) -> list[str]:
:rtype: list(str)
"""
if self.args.arguments: # maybe None with builtin functions
names = _rec_get_names(self.args.arguments)
names = [elt.name for elt in self.args.arguments]
else:
names = []
if self.args.vararg:
Expand Down Expand Up @@ -1246,7 +1246,7 @@ def argnames(self) -> list[str]:
:rtype: list(str)
"""
if self.args.arguments: # maybe None with builtin functions
names = _rec_get_names(self.args.arguments)
names = [elt.name for elt in self.args.arguments]
else:
names = []
if self.args.vararg:
Expand Down Expand Up @@ -1654,18 +1654,6 @@ async def func(things):
"""


def _rec_get_names(args, names: list[str] | None = None) -> list[str]:
"""return a list of all argument names"""
if names is None:
names = []
for arg in args:
if isinstance(arg, node_classes.Tuple):
_rec_get_names(arg.elts, names)
else:
names.append(arg.name)
return names


def _is_metaclass(klass, seen=None, context: InferenceContext | None = None) -> bool:
"""Return if the given class can be
used as a metaclass.
Expand Down
4 changes: 4 additions & 0 deletions tests/test_scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ def test_argnames(self) -> None:
astroid["f"].argnames(), ["a", "b", "args", "c", "d", "kwargs"]
)

def test_argnames_lambda(self) -> None:
lambda_node = extract_node("lambda a, b, c, *args, **kwargs: ...")
self.assertEqual(lambda_node.argnames(), ["a", "b", "c", "args", "kwargs"])

def test_positional_only_argnames(self) -> None:
code = "def f(a, b, /, c=None, *args, d, **kwargs): pass"
astroid = builder.parse(code, __name__)
Expand Down

0 comments on commit 3db2bdd

Please sign in to comment.