Skip to content

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

@lucaswerkmeister

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions