This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improve syntax error for wrongly closed strings
Type: Stage: patch review
Components: Parser Versions: Python 3.11, Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, aroberge, lys.nikolaou, pablogsal
Priority: normal Keywords: patch

Created on 2021-06-09 21:15 by pablogsal, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 26633 open pablogsal, 2021-06-09 22:05
Messages (4)
msg395476 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-06-09 21:15
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.
msg395491 - (view) Author: Andre Roberge (aroberge) * Date: 2021-06-09 23:02
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.
msg395696 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2021-06-12 18:25
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
msg395837 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-06-14 18:54
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

with this PR:

>>> "dfssdfsdfsd" fdsf sdfsdfsd {}
  File "<stdin>", line 1
    "dfssdfsdfsd" fdsf sdfsdfsd {}
                                ^
SyntaxError: invalid syntax

We need a solution to this problem first
History
Date User Action Args
2022-04-11 14:59:46adminsetgithub: 88535
2021-06-14 18:54:32pablogsalsetmessages: + msg395837
2021-06-12 18:25:57BTaskayasetnosy: + BTaskaya
messages: + msg395696
2021-06-09 23:02:14arobergesetnosy: + aroberge
messages: + msg395491
2021-06-09 22:05:13pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request25219
2021-06-09 21:15:52pablogsalcreate