bpo-34013: Move migration suggestion for print expr to parser by ammaraskar · Pull Request #27390 · python/cpython
This is a potential alternative to #27389 that strips out special handling and string parsing for SyntaxErrors from exceptions.c (-200 lines). The grammar is what I threw together quickly, not sure if it's the best to use a soft keyword.
It does not print the exact suggestion anymore but I think with the new caret highlighting of syntax errors this is not a huge concern. In my opinion it's clear what part should be surrounded in parenthesis. Here's a before-after comparison:
>>> print x
File "<stdin>", line 1
print x
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(x)?
>>> print f(3)
File "<stdin>", line 1
print f(3)
^
SyntaxError: invalid syntax
>>> print "Hello", "world", "!"
File "<stdin>", line 1
print "Hello", "world", "!"
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello", "world", "!")?
>>> print "Hello", "world", "!",
File "<stdin>", line 1
print "Hello", "world", "!",
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello", "world", "!", end=" ")?
>>> print x
File "<stdin>", line 1
print x
^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
>>> print f(3)
File "<stdin>", line 1
print f(3)
^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
>>> print "Hello", "world", "!"
File "<stdin>", line 1
print "Hello", "world", "!"
^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
>>> print "Hello", "world", "!",
File "<stdin>", line 1
print "Hello", "world", "!",
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(..., end=' ')?