Skip to content

inspect.getargvalues does not work correctly for pos-only and kw-only arguments #107833

Description

@sobolevn

Here's an example:

>>> fr = None
>>> def a(p, /, a, *, b): 
...     global fr
...     fr = inspect.currentframe()
... 
>>> a(1, 2, b=3)
>>> fr
<frame at 0x101275570, file '<stdin>', line 3, code a>
>>> inspect.getargvalues(fr)
ArgInfo(args=['p', 'a', 'b'], varargs=None, keywords=None, locals={'p': 1, 'a': 2, 'b': 3})

Right now inspect.getargvalues treats p, a, and b the same way. It is not very useful. How caller can do anything with these arguments if they all have different semantics? p must always be possitional, a can be both, b can only be keyword argument.

I propose adding inspect.signature support for frame objects and use it.
For example, adding to Signature class

@classmethod
def from_frame(cls, frame): ...

will provide a new API to do the same thing, but correctly.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions