Skip to content
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

Syntactically wrong suggestions by the new custom print statement error message #76209

Closed
mdraw mannequin opened this issue Nov 14, 2017 · 6 comments
Closed

Syntactically wrong suggestions by the new custom print statement error message #76209

mdraw mannequin opened this issue Nov 14, 2017 · 6 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@mdraw
Copy link
Mannequin

mdraw mannequin commented Nov 14, 2017

BPO 32028
Nosy @ncoghlan, @CuriousLearner, @mdraw
PRs
  • bpo-32028: Fix custom print suggestion having leading whitespace in print statement #4688
  • [3.6] bpo-32028: Fix suggestions for indented print statements (GH-4688) #5249
  • Files
  • print.py: Python file that suggests a wrong print call when run
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-01-20.03:57:45.643>
    created_at = <Date 2017-11-14.17:42:50.183>
    labels = ['interpreter-core', 'type-bug', '3.7']
    title = 'Syntactically wrong suggestions by the new custom print statement error message'
    updated_at = <Date 2018-01-20.03:57:45.641>
    user = 'https://github.com/mdraw'

    bugs.python.org fields:

    activity = <Date 2018-01-20.03:57:45.641>
    actor = 'ncoghlan'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-01-20.03:57:45.643>
    closer = 'ncoghlan'
    components = ['Interpreter Core']
    creation = <Date 2017-11-14.17:42:50.183>
    creator = 'mdraw'
    dependencies = []
    files = ['47265']
    hgrepos = []
    issue_num = 32028
    keywords = ['patch']
    message_count = 6.0
    messages = ['306231', '306253', '306254', '310320', '310321', '310322']
    nosy_count = 3.0
    nosy_names = ['ncoghlan', 'CuriousLearner', 'mdraw']
    pr_nums = ['4688', '5249']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32028'
    versions = ['Python 3.6', 'Python 3.7']

    @mdraw
    Copy link
    Mannequin Author

    mdraw mannequin commented Nov 14, 2017

    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.

    @mdraw mdraw mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Nov 14, 2017
    @ncoghlan
    Copy link
    Contributor

    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)?
    
    

    @ncoghlan ncoghlan added the 3.7 (EOL) end of life label Nov 15, 2017
    @CuriousLearner
    Copy link
    Member

    Sure, let me have a look at it and work on fix.

    @ncoghlan
    Copy link
    Contributor

    New changeset d57f26c by Nick Coghlan (Sanyam Khurana) in branch 'master':
    bpo-32028: Fix suggestions for indented print statements (GH-4688)
    d57f26c

    @ncoghlan
    Copy link
    Contributor

    New changeset 4002d5d by Nick Coghlan (Miss Islington (bot)) in branch '3.6':
    [3.6] bpo-32028: Fix suggestions for indented print statements (GH-5249)
    4002d5d

    @ncoghlan
    Copy link
    Contributor

    Thanks CuriousLearner for the PR and mdraw for the original issue report!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants