Skip to content

Missing error: "parameter without a default follows parameter with a default" with a function that uses *args #129810

Closed as not planned

Description

@adrien-lsh

Bug report

Bug description:

Hey there,

I don't know if that's a bug or if that's expected behavior, but since it's weird I might as well get this checked out.

>>> def f(*args, a = 0, b): pass
... 
>>> def f(a = 0, b): pass
  File "<python-input-1>", line 1
    def f(a = 0, b): pass
                 ^
SyntaxError: parameter without a default follows parameter with a default

I clearly understand the error here as it is not the first time I'm using Python, the thing that eludes me is: why is there no error with the first function ?

Thank you for your help !

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Activity

  1. added
    type-bug
    on Feb 7, 2025
  2. changed the title [-]Missing error: "parameter without a default follows parameter" with a default with a function that uses `*args`[/-] [+]Missing error: "parameter without a default follows parameter with a default" with a function that uses `*args`[/+] on Feb 7, 2025
  3. added
    interpreter-core
    on Feb 7, 2025
  4. sergey-miryanov commented on Feb 7, 2025

    @sergey-miryanov
    Contributor

    I believe this is a desired behavior according https://peps.python.org/pep-3102/:

    Keyword-only arguments are not required to have a default value. Since Python requires that all arguments be bound to a value, and since the only way to bind a value to a keyword-only argument is via keyword, such arguments are therefore ‘required keyword’ arguments. Such arguments must be supplied by the caller, and they must be supplied via keyword.

    So a and b are keyword-only arguments - so user should supply them by keyword. But a has default value and can be omitted from the call.

  5. sergey-miryanov commented on Feb 7, 2025

    @sergey-miryanov
    Contributor

    Maybe we should extend the test_positional_only_and_kwonlyargs_invalid_calls test case and add a keyword-only argument with a default value as an example?

  6. adrien-lsh commented on Feb 7, 2025

    @adrien-lsh
    Author

    Oh ok I get it. It's a bit confusing because we could simply think that b should become a keyword-only argument.
    But since there is no varargs, b is just a positional argument with no default.

    Thanks for the explanation !

  7. terryjreedy commented on Feb 8, 2025

    @terryjreedy
    Member

    I don't know if that's a bug or if that's expected behavior,

    Questions like this should go to https://discuss.python.org/c/help/7, where they are more likely to be found by others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-coretype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions