GeezerVison is showing github.com — Confusing SyntaxError for multi-line string concatenation with missing comma · Issue #142236 · python/cpython
Confusing SyntaxError for multi-line string concatenation with missing comma #142236DescriptionBug 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
Activitylucaswerkmeister commented on Dec 3, 2025 (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 :)) johnslavik commented on Dec 3, 2025 CC @ZeroIntensity (mentorship): That would be topic-parser I think.
I may give this a shot later this week. StanFromIreland commented on Dec 3, 2025 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'?
Reacted by Bartosz Sławecki, Maxim Martynov and Oleksandr Kravets StanFromIreland commented on Dec 3, 2025 Reacted by Bartosz Sławecki johnslavik commented on Dec 3, 2025 StanFromIreland commented on Dec 3, 2025 Let's deal with the first issue for now. lucaswerkmeister commented on Dec 3, 2025
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?
added 5 commits that reference this issue on Dec 6, 2025gh-142236: Improve error location for missing comma in string concate… StanFromIreland commented on Jan 9, 2026 @pablogsal do you want to backport the two PRs? Otherwise there is nothing left to do here. pablogsal commented on Jan 9, 2026 I am torn. @hugovk what do you think? hugovk commented on Jan 11, 2026 I'm fine with backporting, but what did we do with other similar changes in the past? pablogsal commented on Jan 11, 2026 Backport them except when the backport was so different that was risky hugovk commented on Jan 12, 2026 OK, go ahead, unless you think they're risky :) johnslavik commented on Mar 11, 2026 If you'd like to backport, go ahead!
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|