Skip to content

Improve syntax error for wrongly closed strings #88535

Closed

Description

@pablogsal
BPO 44369
Nosy @aroberge, @lysnikolaou, @pablogsal, @isidentical
PRs
  • gh-88535: Improve syntax error for wrongly closed strings #26633
  • 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 = None
    created_at = <Date 2021-06-09.21:15:52.840>
    labels = ['interpreter-core', '3.10', '3.11']
    title = 'Improve syntax error for wrongly closed strings'
    updated_at = <Date 2021-06-14.18:54:32.243>
    user = 'https://github.com/pablogsal'

    bugs.python.org fields:

    activity = <Date 2021-06-14.18:54:32.243>
    actor = 'pablogsal'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Parser']
    creation = <Date 2021-06-09.21:15:52.840>
    creator = 'pablogsal'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44369
    keywords = ['patch']
    message_count = 4.0
    messages = ['395476', '395491', '395696', '395837']
    nosy_count = 4.0
    nosy_names = ['aroberge', 'lys.nikolaou', 'pablogsal', 'BTaskaya']
    pr_nums = ['26633']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue44369'
    versions = ['Python 3.10', 'Python 3.11']

    Activity

    1. pablogsal commented on Jun 9, 2021

      @pablogsal
      MemberAuthor

      Let's asume this string:
      "
      x = "Cannot recover from "MemoryErrors" while something happnes while "

      The line is incorrect because the quotes arround MemoryErrors are the same as the string is used, resulting in STRING + expression + STRING.

      Currenly we say:

      >>> 'Cannot recover from 'MemoryErrors' while '
        File "<stdin>", line 1
          'Cannot recover from 'MemoryErrors' while '
                                ^
      SyntaxError: invalid syntax

      but I think it will be a great improvement if we say:

      >>> 'Cannot recover from 'MemoryErrors' while '
        File "<stdin>", line 1
          'Cannot recover from 'MemoryErrors' while '
                                ^^^^^^^^^^^^
      SyntaxError: invalid syntax. Did you use the same quotes here as the string?

      Probably the message should be better, but I think this is a good improvement.

    2. aroberge commented on Jun 9, 2021

      arobergemannequin
      Mannequin

      I like this. While it would be a bit longer, I'm wondering if the message should not read instead as follows:

      ... Did you use the same quotes here as those enclosing the string?

      ===

      I suspect that errors coming from the use of a single quote, as in:

      'Don't do this'

      might be just as prevalent if not more so; however, they might be more difficult to properly diagnose.

    3. isidentical commented on Jun 12, 2021

      @isidentical
      SponsorMember

      From what I can share by my experience, this error happens soo much. So I'd love to see it.

      For a more consistent message with others, maybe it could be something like this;

      SyntaxError: invalid syntax. Maybe you meant to use a different type of quote

    4. pablogsal commented on Jun 14, 2021

      @pablogsal
      MemberAuthor

      One problem with that is that the lookahead will move the error indicator to some incorrect place if the match fails. For example:

      PYTHON3.9

      >>> "dfssdfsdfsd" fdsf sdfsdfsd {}
        File "<stdin>", line 1
          "dfssdfsdfsd" fdsf sdfsdfsd {}
                        ^
      SyntaxError: invalid syntax
      with this PR:
      >>> "dfssdfsdfsd" fdsf sdfsdfsd {}
        File "<stdin>", line 1
          "dfssdfsdfsd" fdsf sdfsdfsd {}
                                      ^
      SyntaxError: invalid syntax

      We need a solution to this problem first

    5. transferred this issue fromon Apr 10, 2022
    6. added
      type-featureA feature request or enhancement
      and removed
      3.11only security fixes
      on Mar 27, 2024
    7. added a commit that references this issue on Feb 13, 2025
    8. adorilson commented on Jun 25, 2025

      @adorilson
      Contributor

      Could this be closed? It is in whatsnew 3.14.

    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

      interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions