Skip to content

inspect.signature.BoundArguments "POSITIONAL_OR_KEYWORD" Arguments are always args #118577

@Steffenju95

Description

@Steffenju95

Feature Request

Feature description:

from inspect import signature
def foo(bar, /, baz, *, bat):
    ...

ba = signature(foo).bind('bla', baz='bli', bat='blub')

In this case the argument baz get returned with ba.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 and ba.kwargs {bat: 'blub'}.

Is this behavior wanted like this?
In my perspective it makes more sense when the BoundArguments class returns POSITIONAL_OR_KEYWORD arguments as it got it (positional as args and keyword as kwargs) and not always as args. 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:

from inspect import signature
def foo(bar, /, new='blub', baz='bla', *, bat='bli'):
    ...

ba = signature(foo).bind('bla', baz='bli', bat='blub')

ba.args => ('bla',)
ba.kwargs => {'baz': 'bli', 'bat': 'blub'}

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirstdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions