Skip to content

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

Closed

Description

@mdraw
mannequin
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']

    Activity

    1. mdraw commented on Nov 14, 2017

      mdrawmannequin
      MannequinAuthor

      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.

    2. added
      interpreter-core
      type-bug
      on Nov 14, 2017
    3. ncoghlan commented on Nov 15, 2017

      @ncoghlan
      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)?
      
      
    4. CuriousLearner commented on Nov 15, 2017

      @CuriousLearner
      Member

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

    5. ncoghlan commented on Jan 20, 2018

      @ncoghlan
      Contributor

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

    6. ncoghlan commented on Jan 20, 2018

      @ncoghlan
      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

    7. ncoghlan commented on Jan 20, 2018

      @ncoghlan
      Contributor

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

    8. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)interpreter-coretype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions