Skip to content

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

Closed as not planned

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

Activity

  1. hnnynh commented on Aug 15, 2025

    @hnnynh
    Contributor

    Looks Interesting! @corona10 Can you assign this issue to me? It may take some time as this is my first time, but I will do my best. 🚀

  2. corona10 commented on Aug 15, 2025

    @corona10
    Member

    Yeah please go a head!

  3. corona10 commented on Aug 15, 2025

    @corona10
    Member

    @hnnynh Maybe worth to start here:

    | '**' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword parameter cannot have default value") }

  4. skirpichev commented on Aug 15, 2025

    @skirpichev
    Member

    Strictly speaking, error messages are correct. They are talking about default values for parameters, this has clear meaning in the grammar specification. See 8.7. Function definitions:

    When one or more parameters have the form parameter = expression, the function is said to have “default parameter values.” For a parameter with a default value, the corresponding argument may be omitted from a call, in which case the parameter’s default value is substituted.

    I suggest to close this.

  5. added
    pendingThe issue will be closed if no feedback is provided
    on Aug 15, 2025
  6. skirpichev commented on Aug 15, 2025

    @skirpichev
    Member

    CC @pablogsal, as this related to parser

  7. hnnynh commented on Aug 15, 2025

    @hnnynh
    Contributor

    I thought adding a reason to this message would help users better understand the error.
    If the issue is closed, I will close the PR as well.

  8. serhiy-storchaka commented on Aug 15, 2025

    @serhiy-storchaka
    Member

    I think that the current error messages are pretty clear. The proposed error messages are confusing and simply incorrect.

  9. serhiy-storchaka commented on Aug 15, 2025

    @serhiy-storchaka
    Member

    There was an error in these error messages -- the term "argument" was misused instead of "parameter". This is perhaps the reason of your confusion. But this was fixed in #133379.

  10. corona10 commented on Aug 15, 2025

    @corona10
    Member

    Looks like it is not worth to fixing it. Let's close the issue :) Thanks for the effort @hnnynh

  11. moved this from Todo to Done in Sprinton Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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