gh-102551 Fixed inspect.signature.bind behavior on kwargs #102820
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
inspect.signature.bind
is not working properly with keyword arguments on keyword/positional parameters.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 toBoundArguments
to track which arguments are provided as keyword arguments. In the official documentation, we only listedBoundArguments
as a result ofSignature.bind()
orSignature.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.