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: Syntactically wrong suggestions by the new custom print statement error message
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: CuriousLearner, mdraw, ncoghlan
Priority: normal Keywords: patch

Created on 2017-11-14 17:42 by mdraw, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
print.py mdraw, 2017-11-14 17:42 Python file that suggests a wrong print call when run
Pull Requests
URL Status Linked Edit
PR 4688 merged CuriousLearner, 2017-12-03 17:01
PR 5249 merged python-dev, 2018-01-20 03:12
Messages (6)
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) * (Python triager) Date: 2017-11-15 06:43
Sure, let me have a look at it and work on fix.
msg310320 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-01-20 03:12
New changeset d57f26c753dce61f72b52b96db3a3253d9f2fc3e by Nick Coghlan (Sanyam Khurana) in branch 'master':
bpo-32028: Fix suggestions for indented print statements (GH-4688)
https://github.com/python/cpython/commit/d57f26c753dce61f72b52b96db3a3253d9f2fc3e
msg310321 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-01-20 03:56
New changeset 4002d5dbf4c058bbf2462f9f5dea057956d1caff by Nick Coghlan (Miss Islington (bot)) in branch '3.6':
[3.6] bpo-32028: Fix suggestions for indented print statements (GH-5249)
https://github.com/python/cpython/commit/4002d5dbf4c058bbf2462f9f5dea057956d1caff
msg310322 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-01-20 03:57
Thanks CuriousLearner for the PR and mdraw for the original issue report!
History
Date User Action Args
2022-04-11 14:58:54adminsetgithub: 76209
2018-01-20 03:57:45ncoghlansetstatus: open -> closed
resolution: fixed
messages: + msg310322

stage: patch review -> resolved
2018-01-20 03:56:33ncoghlansetmessages: + msg310321
2018-01-20 03:12:32python-devsetpull_requests: + pull_request5096
2018-01-20 03:12:24ncoghlansetmessages: + msg310320
2017-12-03 17:01:55CuriousLearnersetkeywords: + patch
stage: test needed -> patch review
pull_requests: + pull_request4601
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