You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve syntax error for wrongly closed strings #88535
Let's asume this string:
"
x = "Cannot recover from "MemoryErrors" while something happnes while "
The line is incorrect because the quotes arround MemoryErrors are the same as the string is used, resulting in STRING + expression + STRING.
Currenly we say:
>>> 'Cannot recover from 'MemoryErrors' while '
File "<stdin>", line 1
'Cannot recover from 'MemoryErrors' while '
^
SyntaxError: invalid syntax
but I think it will be a great improvement if we say:
>>> 'Cannot recover from 'MemoryErrors' while '
File "<stdin>", line 1
'Cannot recover from 'MemoryErrors' while '
^^^^^^^^^^^^
SyntaxError: invalid syntax. Did you use the same quotes here as the string?
Probably the message should be better, but I think this is a good improvement.
Activity
pablogsal commentedon Jun 9, 2021
Let's asume this string:
"
x = "Cannot recover from "MemoryErrors" while something happnes while "
The line is incorrect because the quotes arround MemoryErrors are the same as the string is used, resulting in STRING + expression + STRING.
Currenly we say:
but I think it will be a great improvement if we say:
Probably the message should be better, but I think this is a good improvement.
aroberge commentedon Jun 9, 2021
I like this. While it would be a bit longer, I'm wondering if the message should not read instead as follows:
... Did you use the same quotes here as those enclosing the string?
===
I suspect that errors coming from the use of a single quote, as in:
'Don't do this'
might be just as prevalent if not more so; however, they might be more difficult to properly diagnose.
isidentical commentedon Jun 12, 2021
From what I can share by my experience, this error happens soo much. So I'd love to see it.
For a more consistent message with others, maybe it could be something like this;
SyntaxError: invalid syntax. Maybe you meant to use a different type of quote
pablogsal commentedon Jun 14, 2021
One problem with that is that the lookahead will move the error indicator to some incorrect place if the match fails. For example:
PYTHON3.9
>>> "dfssdfsdfsd" fdsf sdfsdfsd {} File "<stdin>", line 1 "dfssdfsdfsd" fdsf sdfsdfsd {} ^ SyntaxError: invalid syntax>>> "dfssdfsdfsd" fdsf sdfsdfsd {} File "<stdin>", line 1 "dfssdfsdfsd" fdsf sdfsdfsd {} ^ SyntaxError: invalid syntaxWe need a solution to this problem first
gh-88535: Improve syntax error for wrongly closed strings (#26633)
adorilson commentedon Jun 25, 2025
Could this be closed? It is in whatsnew 3.14.