-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
inspect.signature.BoundArguments "POSITIONAL_OR_KEYWORD" Arguments are always args #118577
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
Comments
Running your copy-pasted code in current 3.11.9, 3.12.3, and 3.13.0a6 on Windows, |
Thank you for you fast reply. You are absolutely right, It's behaves like you said. I should next time check more carefully and copy paste my tested code. But one point stays. I still think I'm just not sure if it's still a bug report or more a feature request. I guess it's depends on, if the actual behavior is intended as it is. |
See also #102551 |
Okay, so it's definitely intended as it is. (I missed that in the PEP 362)
But if there are good arguments against it, I think it would be a good idea to address this unintuitive behaviour beside the one sentence in the PEP also in the code doc and here: https://docs.python.org/3/library/inspect.html#inspect.BoundArguments.args Edit: |
Please suggest specific doc changes in a comment. |
args kwargs Note: The allocation in args and kwargs may not match the inserted args and kwargs in Signature.bind() or Signature.bind_partial(). This concerns argument of the kind POSITIONAL_OR_KEYWORD in cases where they can be passed ambiguous as args or kwargs. In cases of ambiguity the dynamically computation of args and kwargs always simplifies the given arguments as much as possible, by dropping keywords and saving them positional in args. For example: def test(a=1, b=2, c=3):
pass
sig = signature(test)
ba = sig.bind(a=10, c=13)
>>> ba.args
(10,)
>>> ba.kwargs:
{'c': 13} |
…alues in args (pythonGH-119936) (cherry picked from commit 8e99495) Co-authored-by: Nice Zombies <[email protected]>
…alues in args (pythonGH-119936) (cherry picked from commit 8e99495) Co-authored-by: Nice Zombies <[email protected]>
…values in args (GH-119936) (GH-124004) (cherry picked from commit 8e99495) Co-authored-by: Nice Zombies <[email protected]>
Thanks @nineteendo for the PR! |
Feature Request
Feature description:
In this case the argument
baz
get returned withba.args
, which returns('bla', 'bli')
I expected it in
ba.kwargs
, but that returns only{bat: 'blub'}
.Binding
baz
positional (bind('bla', 'bli', bat='blub')
) works as expected.Here
ba.args
returns('bla', 'bli')
again andba.kwargs
{bat: 'blub'}
.Is this behavior wanted like this?
In my perspective it makes more sense when the
BoundArguments
class returnsPOSITIONAL_OR_KEYWORD
arguments as it got it (positional asargs
and keyword askwargs
) and not always asargs
. I think there happens a lost of Information .Edit:
At least it not cause errors, because in constellations, where it is necessary to be a keyword it is. So it also could argued against the information lost, that it is a build in feature of simplification of unnecessary keywords.
See the following example:
ba.args
=>('bla',)
ba.kwargs
=>{'baz': 'bli', 'bat': 'blub'}
CPython versions tested on:
3.12
Operating systems tested on:
Windows
Linked PRs
The text was updated successfully, but these errors were encountered: