Skip to content

Inconsistent keyword argument validation for integer keys #141943

@PelinV

Description

@PelinV

Bug report

Bug description:

There is inconsistent behavior in Python's keyword argument handling when using dictionary unpacking with string keys that contain only digits.

def f(**kwargs):
    return kwargs

# These correctly raise error as expected
f(0=0)  # SyntaxError: keyword can't be an expression
f(**{0: 0})  # TypeError: f() keywords must be strings

# These should raise errors but don't
f(**{'0': 0})  # returns: {'0': 0} - UNEXPECTED
f(**{'0': 0, 'a': 1, 'b': 2})  # returns: {'0': 0, 'a': 1, 'b': 2} - UNEXPECTED

All four calls should behave consistently. Since f(0=0) and f({0: 0}) raise errors (because integer keys are invalid as keyword argument names), f({'0': 0}) should also raise an error because the string '0' cannot be a valid Python identifier for a keyword argument.
I expect to be able to get the same kwargs when migrating them with and without dictionary unpacking.
I can only get {'0':0} when using dictionary unpacking viaf(**{'0': 0})
And I can't get this result without unpacking via f(0=0)

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions