Skip to content

Commit 26c54d2

Browse files
Include names of keyword-only arguments in argnames() (#1382)
1 parent b8c3683 commit 26c54d2

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Release date: TBA
1515

1616
Closes PyCQA/pylint#5679
1717

18+
* Inlcude names of keyword-only arguments in ``astroid.scoped_nodes.Lambda.argnames``.
19+
20+
Closes PyCQA/pylint#5771
21+
1822
* Add support for [attrs v21.3.0](https://github.com/python-attrs/attrs/releases/tag/21.3.0) which
1923
added a new `attrs` module alongside the existing `attr`.
2024

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,8 +1366,10 @@ def callable(self):
13661366
"""
13671367
return True
13681368

1369-
def argnames(self):
1370-
"""Get the names of each of the arguments.
1369+
def argnames(self) -> List[str]:
1370+
"""Get the names of each of the arguments, including that
1371+
of the collections of variable-length arguments ("args", "kwargs",
1372+
etc.), as well as keyword-only arguments.
13711373
13721374
:returns: The names of the arguments.
13731375
:rtype: list(str)
@@ -1376,6 +1378,7 @@ def argnames(self):
13761378
names = _rec_get_names(self.args.arguments)
13771379
else:
13781380
names = []
1381+
names += [elt.name for elt in self.args.kwonlyargs]
13791382
if self.args.vararg:
13801383
names.append(self.args.vararg)
13811384
if self.args.kwarg:
@@ -1991,7 +1994,7 @@ async def func(things):
19911994
"""
19921995

19931996

1994-
def _rec_get_names(args, names=None):
1997+
def _rec_get_names(args, names: Optional[List[str]] = None) -> List[str]:
19951998
"""return a list of all argument names"""
19961999
if names is None:
19972000
names = []

tests/unittest_scoped_nodes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,10 @@ def test_argnames(self) -> None:
473473
astroid = builder.parse(code, __name__)
474474
self.assertEqual(astroid["f"].argnames(), ["a", "b", "c", "args", "kwargs"])
475475

476+
code_with_kwonly_args = "def f(a, b, *, c=None, d=None): pass"
477+
astroid = builder.parse(code_with_kwonly_args, __name__)
478+
self.assertEqual(astroid["f"].argnames(), ["a", "b", "c", "d"])
479+
476480
def test_return_nothing(self) -> None:
477481
"""test inferred value on a function with empty return"""
478482
data = """

0 commit comments

Comments
 (0)