classification
Title: Syntactically wrong suggestions by the new custom print statement error message
Type: behavior Stage: test needed
Components: Interpreter Core Versions: Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: CuriousLearner, mdraw, ncoghlan
Priority: normal Keywords:

Created on 2017-11-14 17:42 by mdraw, last changed 2017-11-15 06:43 by CuriousLearner.

Files
File name Uploaded Description Edit
print.py mdraw, 2017-11-14 17:42 Python file that suggests a wrong print call when run
Messages (3)
msg306231 - (view) Author: Martin Drawitsch (mdraw) Date: 2017-11-14 17:42
I think I found a bug in the new print syntax suggestion introduced by https://bugs.python.org/issue30597.


When the following code is executed by Python 3.6.3 inside of a .py file:

    def f():
        print '%d' % 2

, then Python gives the following error message:

    SyntaxError: Missing parentheses in call to 'print'. Did you mean print(int '%d' % 2)?

The "int" next to the left brace of the suggested print function is obviously wrong.
The expected message would be:

    SyntaxError: Missing parentheses in call to 'print'. Did you mean print('%d' % 2)?

Using other values or "%s" in the formatted string in a print statement produces the same wrong message.
This bug only seems to happen when the print statement is inside of a function AND when it is is run inside of a .py file. At least I could not reproduce it in the python3 REPL or outside of a function.

I am attaching the minimal example file in this bug report. Running it with "$ python3 print.py" should show the mentioned bug.
msg306253 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-11-15 06:35
Given the symptoms (stripping 4 spaces + "pr" from the start of the line, leaving "int " behind), it looks like we're not stripping the leading whitespace when determining the text to include in the suggested print() call.

To reproduce this at the REPL, you can use an if statement (first example uses a 4 space indent, the second uses an 8 space indent):

```
>>> if 1:
...     print 123
  File "<stdin>", line 2
    print 123
            ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(int 123)?
>>> if 1:
...         print 123
  File "<stdin>", line 2
    print 123
            ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(print 123)?

```
msg306254 - (view) Author: Sanyam Khurana (CuriousLearner) * Date: 2017-11-15 06:43
Sure, let me have a look at it and work on fix.
History
Date User Action Args
2017-11-15 06:43:16CuriousLearnersetmessages: + msg306254
2017-11-15 06:35:19ncoghlansetstage: test needed
messages: + msg306253
versions: + Python 3.7
2017-11-14 17:42:50mdrawcreate