Skip to content

The error messages of def func(*args='Hello'): pass and def func(**kwargs='Hello'): pass should explain more clearly #137786

@hyperkai

Description

@hyperkai

Bug report

Bug description:

*args and **kwargs have an empty tuple and dictionary respectively as default values as shown below:

def func(*args):
    print(args, type(args))

func()
# () <class 'tuple'>
def func(**kwargs):
    print(kwargs, type(kwargs))

func()
{} <class 'dict'>

Then, setting a default value to *args and **kwargs gets the error messages as shown below. I think the error messages should explain more clearly because Python doc doesn't explain *args and **kwargs are the optional parameters which have the immutable default values () and {} respectively and var-keyword argument and var-keyword argument should be var-keyword parameter and var-keyword parameter:

def func(*args='Hello'):
    pass
# Error

SyntaxError: var-positional argument cannot have default value

def func(**kwargs='Hello'):
    pass
# Error

SyntaxError: var-keyword argument cannot have default value

So, the error messages should be like belolw:

SyntaxError: var-positional parameter's default value cannot be changed because it has the immutable default value ()

SyntaxError: var-keyword parameter's default value cannot be changed because it has the immutable default value {}

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

Labels

pendingThe issue will be closed if no feedback is providedsprinttype-bugAn unexpected behavior, bug, or error

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions