8000 "SyntaxError: non-default argument follows default argument" confuses · Issue #91210 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

"SyntaxError: non-default argument follows default argument" confuses #91210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Bluenix2 mannequin opened this issue Mar 18, 2022 · 7 comments
Closed

"SyntaxError: non-default argument follows default argument" confuses #91210

Bluenix2 mannequin opened this issue Mar 18, 2022 · 7 comments
Assignees
Labels
3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@Bluenix2
Copy link
Mannequin
Bluenix2 mannequin commented Mar 18, 2022
BPO 47054
Nosy @terryjreedy, @gpshead, @vedgar, @pablogsal, @Bluenix2
PRs
  • bpo-47054: Call params parameters not arguments in SyntaxErrors. #32014
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/gpshead'
    closed_at = None
    created_at = <Date 2022-03-18.01:17:42.872>
    labels = ['interpreter-core', 'type-bug', '3.10', '3.11']
    title = '"SyntaxError: non-default argument follows default argument" confuses'
    updated_at = <Date 2022-03-20.21:18:26.112>
    user = 'https://github.com/Bluenix2'

    bugs.python.org fields:

    activity = <Date 2022-03-20.21:18:26.112>
    actor = 'gregory.p.smith'
    assignee = 'gregory.p.smith'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2022-03-18.01:17:42.872>
    creator = 'bluenix'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 47054
    keywords = ['patch']
    message_count = 4.0
    messages = ['415463', '415469', '415526', '415530']
    nosy_count = 5.0
    nosy_names = ['terry.reedy', 'gregory.p.smith', 'veky', 'pablogsal', 'bluenix']
    pr_nums = ['32014']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue47054'
    versions = ['Python 3.10', 'Python 3.11']

    @Bluenix2
    Copy link
    Mannequin Author
    Bluenix2 mannequin commented Mar 18, 2022

    Running the code below will produce a SyntaxError with the message: "non-default argument follows default argument".

        def test(a=None, b):
            return b if a is None else a

    The issue is that a and b aren't arguments, rather, they are parameters. The error message should be changed to: "non-default parameter follows default parameter".

    This change appears simple enough and although it is not something I've found to be troublesome, it will help users to use correct keywords (arguments vs. parameters) when searching on the internet.

    @Bluenix2 Bluenix2 mannequin added 3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Mar 18, 2022
    @vedgar
    Copy link
    Mannequin
    vedgar mannequin commented Mar 18, 2022

    The problem is more subtle. The thing is, "default parameter" doesn't make sense in this context. Yes, a and b are parameter, but a is not "default parameter" in any sensible way. It is _None_ which is the default argument for parameter a, while parameter b has no default argument. In other words, if the function weren't called with a first argument, the default argument would be used.

    So, the fix should be a bit more complicated. Maybe, "a parameter without a default follows a parameter with a default"?

    @Bluenix2
    Copy link
    Mannequin Author
    Bluenix2 mannequin commented Mar 18, 2022

    Yes I agree, that would be the best.

    @terryjreedy
    Copy link
    Member

    Current message same in 3.11 and 3.9, but I am not sure about backporting to old parser. However merges a change, if any, can decide.

    @terryjreedy terryjreedy added 3.11 only security fixes type-bug An unexpected behavior, bug, or error and removed 3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes labels Mar 19, 2022
    @terryjreedy terryjreedy changed the title "SyntaxError: non-default argument follows default argument" should be "parameter" "SyntaxError: non-default argument follows default argument" confuses Mar 19, 2022
    @terryjreedy terryjreedy changed the title "SyntaxError: non-default argument follows default argument" should be "parameter" "SyntaxError: non-default argument follows default argument" confuses Mar 19, 2022
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @lysnikolaou
    Copy link
    Member

    In addition to this, we fall back to the generic invalid syntax message, in case there are positional-only parameters before the failing ones:

    >>> def test(a=None, b):
      File "<stdin>", line 1
        def test(a=None, b):
                         ^
    SyntaxError: non-default argument follows default argument
    >>> def test(a, /, b=None, c):
      File "<stdin>", line 1
        def test(a, /, b=None, c):
                                ^
    SyntaxError: invalid syntax

    @gpshead you still intend to work on this? I can open a PR for it in case you won't.

    @gpshead
    Copy link
    Member
    gpshead commented Aug 12, 2022

    I've lost track of why I assigned this to myself (I didn't leave myself a comment? tsk tsk), so no, feel free to take it!

    @gpshead gpshead removed their assignment Aug 12, 2022
    @lysnikolaou lysnikolaou self-assigned this Aug 12, 2022
    lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Aug 12, 2022
    …default
    
    - Improve error message when parameter without a default follows one
      with a default
    - Show same error message when positional-only params precede the
      default/non-default sequence
    miss-islington pushed a commit that referenced this issue Sep 17, 2022
    GH-95933)
    
    - Improve error message when parameter without a default follows one with a default
    - Show same error message when positional-only params precede the default/non-default sequence
    @hauntsaninja
    Copy link
    Contributor

    Thanks, looks like this has been fixed :-)

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants
    0