Skip to content

gh-88535: Improve syntax error for wrongly closed strings #26633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Improved error messages
error message prints the received number of values in more cases than before.
(Contributed by Tushar Sadhwani in :gh:`122239`.)

.. code-block:: pycon
.. code-block:: python

>>> x, y, z = 1, 2, 3, 4
Traceback (most recent call last):
Expand All @@ -175,6 +175,17 @@ Improved error messages
ValueError: too many values to unpack (expected 3, got 4)


* When incorrectly closed strings are detected, the error message suggests
that the string may be intended to be part of the string. (Contributed by
Pablo Galindo in :gh:`88535`.)

.. code-block:: python

>>> "The interesting object "The important object" is very important"
Traceback (most recent call last):
SyntaxError: invalid syntax. Is this intended to be part of the string?


.. _whatsnew314-pep741:

PEP 741: Python Configuration C API
Expand Down
3 changes: 3 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,9 @@ invalid_type_param:
}

invalid_expression:
| STRING a=(!STRING expression_without_invalid)+ STRING {
RAISE_SYNTAX_ERROR_KNOWN_RANGE( PyPegen_first_item(a, expr_ty), PyPegen_last_item(a, expr_ty),
"invalid syntax. Is this intended to be part of the string?") }
# !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
# Soft keywords need to also be ignored because they can be parsed as NAME NAME
| !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2303,7 +2303,7 @@ def test_encodings(self):
)
err = run_script(source.encode('cp437'))
self.assertEqual(err[-3], ' "¢¢¢¢¢¢" + f(4, x for x in range(1))')
self.assertEqual(err[-2], ' ^^^^^^^^^^^^^^^^^^^')
self.assertEqual(err[-2], ' ^^^')

# Check backwards tokenizer errors
source = '# -*- coding: ascii -*-\n\n(\n'
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@
Traceback (most recent call last):
SyntaxError: did you forget parentheses around the comprehension target?

# Incorrectly closed strings

>>> "The interesting object "The important object" is very important"
Traceback (most recent call last):
SyntaxError: invalid syntax. Is this intended to be part of the string?

# Missing commas in literals collections should not
# produce special error messages regarding missing
# parentheses, but about missing commas instead
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve syntax errors for incorrectly closed strings. Patch by Pablo Galindo
Loading
Loading