Skip to content

gh-102551 Fixed inspect.signature.bind behavior on kwargs #102820

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

Closed
wants to merge 3 commits into from

Conversation

gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Mar 18, 2023

inspect.signature.bind is not working properly with keyword arguments on keyword/positional parameters.

import inspect

def add(a, b, c=100):
    return a + b + c

bound = inspect.signature(add).bind(1, 2, c=100)
print(bound.args)

The result should be (1, 2), not (1, 2, 100)

However, we simply do not have enough information in BoundArguments to figure out if an argument is provided as keyword argument, if that parameter itself could be keyword and positional, and it's right after positional arguments(no such parameter is skipped).

In this PR, a new member kwarg_names is introduced to BoundArguments to track which arguments are provided as keyword arguments. In the official documentation, we only listed BoundArguments as a result of Signature.bind() or Signature.bind_partial(), so it should be fine if we added an extra parameter. Also the parameter is optional so it won't break the code that has attempted to use it.

A wrong test in test_inspect is also fixed to the correct behavior.

However, there are a couple of tests in test_unittest relies on the potentially incorrect fact that (1, 2) and (a=1, b=2) are the same arguments. We need to discuss that part - whether to change the test to "correct" behavior(which is how it's used in the official docs), or change the unittest library to accommodate the new changes to keep the old behavior.

Lib/inspect.py Outdated
Comment on lines 2840 to 2843
def __init__(self, signature, arguments, kwarg_names={}):
self.arguments = arguments
self._signature = signature
self._kwarg_names = kwarg_names
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __init__(self, signature, arguments, kwarg_names={}):
self.arguments = arguments
self._signature = signature
self._kwarg_names = kwarg_names
def __init__(self, signature, arguments, kwarg_names=None):
self.arguments = arguments
self._signature = signature
self._kwarg_names = kwarg_names or {}

Should not use mutable instances as function defaults.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, don't know what I was thinking about then..

@gaogaotiantian
Copy link
Member Author

This contradicts PEP 362 so close it.

@gaogaotiantian gaogaotiantian deleted the gh-102551 branch April 4, 2023 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants