pylint-dev/astroid

`.arguments` property ignores keyword-only args, *args, and **kwargs

jacobtylerwalls opened this issue · 5 comments

>>> from astroid import extract_node
>>> node = extract_node("""def a(*args, b=None, c=None, **kwargs): ...""")
>>> node.args.arguments
[]

Expected to find all the arguments from the function signature.

The wanted data can be found here:

>>> node.args.vararg
'args'
>>> node.args.kwarg
'kwargs'
>>> node.args.kwonlyargs
[<AssignName.b l.1 at 0x1048189b0>, <AssignName.c l.1 at 0x104818830>]

Discussed at pylint-dev/pylint#7577 (comment).

Notice that positional-only args are found for some reason 🤷

Should the definition be changed as well? It states args.arguments returns required arguments, and AFAIK in the example none are required (I can call a without supplying any arguments).

I tried running the following:

>>> node = extract_node("""def a(kiwi, apple, *args, b=None, c=None, **kwargs): ...""")
>>> node.args.arguments
[<AssignName.kiwi l.1 at 0x7f5c55986b90>, <AssignName.apple l.1 at 0x7f5c55985a50>]

And it seems correct to me 🤔

@cached_property
def arguments(self):
"""Get all the arguments for this node, including positional only and positional and keyword"""
return list(itertools.chain((self.posonlyargs or ()), self.args or ()))

The docstring seems to be correct?

Depends on how you parse the language. "positional and keyword" could describe the argument kiwi and exclude keyword-only arguments.

Essentially, the crux of this is whether we should

  • leave the function as is, and audit everywhere that uses it (given that we keep finding bugs)
  • change the function

@crazybolillo have you happened to sample the places that use this function to be able to offer a view on that? I'd be eager to hear it!

I think I got confused about the documentation 😭. I was reading the docstring for args (node.args.args in the example):

args: list[AssignName] | None
"""The names of the required arguments.

but we are dealing with arguments (node.args.arguments). I will review the code further to see if I can come up with something

I also misunderstood the documentation or it's wrong and #2240 is the fix.