Skip to content

Confusing SyntaxError for multi-line string concatenation with missing comma #142236

Description

@lucaswerkmeister

Bug report

Bug description:

I just spent a couple of minutes figuring out what was wrong with this code:

import sys

print(
    "\n"
    "Notice: a Git repository with 'main' and 'doc' branches has been set up.\n"
    "You will probably want to amend the initial commit on the 'main' branch.\n"
    file=sys.stderr
)

It produces the following error:

  File "/tmp/tmp.py", line 4
    "\n"
    ^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

Which had me looking at the "\n" line and trying to confirm if Python even has string literal concatenation like I seemed to remember (it does). The error was actually two lines further down, at the end of the concatenated string literal: that line was missing the comma (fix).

Would it be possible to improve the error message, moving it to the last line of the string literal that doesn’t have a comma?

CPython versions tested on:

3.14, 3.13, 3.15

Operating systems tested on:

Linux

Linked PRs

Activity

  1. lucaswerkmeister commented on Dec 3, 2025

    @lucaswerkmeister
    Author

    (Not sure if “bug” is the right type for this, feel free to re-label – or close, for that matter. But I thought you might want to hear about a confusing error message :))

  2. johnslavik commented on Dec 3, 2025

    @johnslavik
    Member

    CC @ZeroIntensity (mentorship): That would be topic-parser I think.

    I may give this a shot later this week.

  3. StanFromIreland commented on Dec 3, 2025

    @StanFromIreland
    Member

    The suggestion I get on main in the repl is even worse:

      File "<python-input-0>", line 4
        print(
        ^^^^^
    SyntaxError: invalid syntax. Did you mean 'not'?
    
  4. StanFromIreland commented on Dec 3, 2025

    @StanFromIreland
    Member

    It's traceback.py. CC @pablogsal

  5. johnslavik commented on Dec 3, 2025

    @johnslavik
    Member

    @StanFromIreland If it's traceback.py, then maybe let's also add stdlib.

  6. StanFromIreland commented on Dec 3, 2025

    @StanFromIreland
    Member

    Let's deal with the first issue for now.

  7. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Dec 3, 2025
  8. lucaswerkmeister commented on Dec 3, 2025

    @lucaswerkmeister
    Author

    The suggestion I get on main in the repl is even worse:

      File "<python-input-0>", line 4
        print(
        ^^^^^
    SyntaxError: invalid syntax. Did you mean 'not'?
    

    FWIW, I got a similar error earlier when I tried out the input in a REPL, but the previous error when evaluating a .py file.

    shell session
    $ docker run --rm -it --entrypoint=bash python:3.15-rc-trixie
    root@6282dd65901a:/# python
    Python 3.15.0a2 (main, Nov 19 2025, 18:40:55) [GCC 14.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    ... 
    ... print(
    ...     "\n"
    ...     "Notice: a Git repository with 'main' and 'doc' branches has been set up.\n"
    ...     "You will probably want to amend the initial commit on the 'main' branch.\n"
    ...     file=sys.stderr
    ... )
    ... 
      File "<python-input-0>", line 4
        print(
        ^^^^^
    SyntaxError: invalid syntax. Did you mean 'not'?
    >>> 
    root@6282dd65901a:/# cat >| /tmp/tmp.py
    import sys
    
    print(
        "\n"
        "Notice: a Git repository with 'main' and 'doc' branches has been set up.\n"
        "You will probably want to amend the initial commit on the 'main' branch.\n"
        file=sys.stderr
    )
    root@6282dd65901a:/# python /tmp/tmp.py
      File "/tmp/tmp.py", line 4
        "\n"
        ^^^^
    SyntaxError: invalid syntax. Perhaps you forgot a comma?
    
  9. added 3 commits that reference this issue on Dec 6, 2025
  10. 1 remaining item

  11. added 5 commits that reference this issue on Dec 6, 2025
  12. added a commit that references this issue on Dec 12, 2025
  13. StanFromIreland commented on Jan 9, 2026

    @StanFromIreland
    Member

    @pablogsal do you want to backport the two PRs? Otherwise there is nothing left to do here.

  14. added
    pendingThe issue will be closed if no feedback is provided
    on Jan 9, 2026
  15. pablogsal commented on Jan 9, 2026

    @pablogsal
    Member

    I am torn. @hugovk what do you think?

  16. hugovk commented on Jan 11, 2026

    @hugovk
    Member

    I'm fine with backporting, but what did we do with other similar changes in the past?

  17. pablogsal commented on Jan 11, 2026

    @pablogsal
    Member

    Backport them except when the backport was so different that was risky

  18. hugovk commented on Jan 12, 2026

    @hugovk
    Member

    OK, go ahead, unless you think they're risky :)

  19. johnslavik commented on Mar 11, 2026

    @johnslavik
    Member

    If you'd like to backport, go ahead!

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-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedtopic-parsertype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions