Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sobolevn opened this issue Aug 10, 2023 · 1 comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

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.

@sobolevn sobolevn added the type-bug An unexpected behavior, bug, or error label Aug 10, 2023
@sobolevn
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants