Skip to content

Improve some re error messages using regex for hints #66560

Closed

Description

@serhiy-storchaka
BPO 22364
Nosy @rhettinger, @terryjreedy, @pitrou, @ezio-melotti, @stevendaprano, @serhiy-storchaka
Dependencies
  • bpo-22578: Add additional attributes to re.error
  • bpo-22581: Other mentions of the buffer protocol
  • Files
  • re_errors_regex.patch
  • re_errors.patch
  • re_errors_diff.txt
  • re_errors_2.patch
  • regex_errors.diff
  • re_errors_3.patch
  • regex_errors2.diff
  • 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 = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2015-03-25.19:05:44.319>
    created_at = <Date 2014-09-08.12:22:56.243>
    labels = ['expert-regex', 'type-feature', 'library']
    title = 'Improve some re error messages using regex for hints'
    updated_at = <Date 2015-03-25.19:05:44.318>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2015-03-25.19:05:44.318>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2015-03-25.19:05:44.319>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)', 'Regular Expressions']
    creation = <Date 2014-09-08.12:22:56.243>
    creator = 'serhiy.storchaka'
    dependencies = ['22578', '22581']
    files = ['37167', '38035', '38036', '38080', '38171', '38229', '38230']
    hgrepos = []
    issue_num = 22364
    keywords = ['patch']
    message_count = 27.0
    messages = ['226575', '226576', '226599', '226635', '226641', '226790', '226847', '227065', '227084', '227119', '227130', '227132', '228599', '230464', '230481', '230965', '231057', '235532', '235534', '235678', '236188', '236201', '236257', '236549', '236551', '236954', '239279']
    nosy_count = 9.0
    nosy_names = ['rhettinger', 'terry.reedy', 'pitrou', 'ezio.melotti', 'mrabarnett', 'steven.daprano', 'BreamoreBoy', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue22364'
    versions = ['Python 3.5']

    Activity

    1. serhiy-storchaka commented on Sep 8, 2014

      @serhiy-storchaka
      MemberAuthor

      In some cases standard re module and third-party regex modules raise exceptions with different error messages.

      1. re.match(re.compile('.'), 'A', re.I)

      re: Cannot process flags argument with a compiled pattern
      regex: can't process flags argument with a compiled pattern

      1. re.compile('(?P<foo_123')

      re: unterminated name
      regex: missing >

      1. re.compile('(?P<foo_123>a)(?P=foo_123')

      re: unterminated name
      regex: missing )

      1. regex.sub('(?P<a>x)', r'\g<a', 'xx')

      re: unterminated group name
      regex: missing >

      1. re.sub('(?P<a>x)', r'\g<', 'xx')

      re: unterminated group name
      regex: bad group name

      1. re.sub('(?P<a>x)', r'\g<a a>', 'xx')

      re: bad character in group name
      regex: bad group name

      1. re.sub('(?P<a>x)', r'\g<-1>', 'xx')

      re: negative group number
      regex: bad group name

      1. re.compile('(?P<foo_123>a)(?P=!)')

      re: bad character in backref group name '!'
      regex: bad group name

      1. re.sub('(?P<a>x)', r'\g', 'xx')

      re: missing group name
      regex: missing <

      1. re.compile('a\\')
        re.sub('x', '\\', 'x')

      re: bogus escape (end of line)
      regex: bad escape

      1. re.compile(r'\1')

      re: bogus escape: '\1'
      regex: unknown group

      1. re.compile('[a-')

      re: unexpected end of regular expression
      regex: bad set

      1. re.sub(b'.', 'b', b'c')

      re: expected bytes, bytearray, or an object with the buffer interface, str found
      regex: expected bytes instance, str found

      1. re.compile(r'\w', re.UNICODE | re.ASCII)

      re: ASCII and UNICODE flags are incompatible
      regex: ASCII, LOCALE and UNICODE flags are mutually incompatible

      1. re.compile('(abc')

      re: unbalanced parenthesis
      regex: missing )

      1. re.compile('abc)')

      re: unbalanced parenthesis
      regex: trailing characters in pattern

      1. re.compile(r'((.)\1+)')

      re: cannot refer to open group
      regex: can't refer to an open group

      Looks as in one case re messages are better, and in other cases regex messages are better. In any case it would be good to unify error messages in both modules.

    2. serhiy-storchaka commented on Sep 8, 2014

      @serhiy-storchaka
      MemberAuthor
      1. re.compile(r'.???')

      re: multiple repeat
      regex: nothing to repeat at position 3

    3. stevendaprano commented on Sep 8, 2014

      @stevendaprano
      Member

      I'm dubious about this issue. It suggests that the wording of the exceptions is part of the API of the two modules.

      If the idea is just to copy the best error messages from one module to the other, then I guess there is no harm. But if the idea is to guarantee to keep the two modules' messages in sync, then I think it is unnecessary and harmful.

    4. serhiy-storchaka commented on Sep 9, 2014

      @serhiy-storchaka
      MemberAuthor

      Yes, the idea of this issue is to enhance the re module (and the regex module if Matthew will) be picking the best error messages (or writing a new one).

    5. mrabarnett commented on Sep 9, 2014

      mrabarnettmannequin
      Mannequin

      re: Cannot process flags argument with a compiled pattern
      regex: can't process flags argument with a compiled pattern

      Error messages usually start with a lowercase letter, and I think that all the other ones in the re module do.

      By the way, which is preferred, "cannot" or "can't"? The regex module always uses "can't", but re module uses "cannot" except for "TypeError: can't use a bytes pattern on a string-like object", I think.

      Also, you said that one of the re module's messages was better, but didn't say which! Did you mean this one?

      re: expected bytes, bytearray, or an object with the buffer interface, str found
      regex: expected bytes instance, str found

    6. serhiy-storchaka commented on Sep 11, 2014

      @serhiy-storchaka
      MemberAuthor

      By the way, which is preferred, "cannot" or "can't"? The regex module always
      uses "can't", but re module uses "cannot" except for "TypeError: can't use
      a bytes pattern on a string-like object", I think.

      It's interesting question. Grepping in CPython sources got results:

      Cannot 210
      cannot 865
      Can't 216
      can't 796

      Lowercase wins uppercase with score 4:1 and short and long forms are
      equivalent.

      I left the decision to English speakers.

      Also, you said that one of the re module's messages was better, but didn't
      say which! Did you mean this one?
      > re: expected bytes, bytearray, or an object with the buffer interface,
      > str found
      > regex: expected bytes instance, str found

      Both are not good. re variant is too verbose, but it is more correct.

      May be 6, 7, 8, 10, 11, 16, 18 are better in re.

    7. terryjreedy commented on Sep 12, 2014

      @terryjreedy
      Member

      I prefer "cannot" for error messages. "Can't" is an informal version of "cannot", used in speech, dialog representing speech, and 'informal' writing. It looks wrong to me in this context.

    8. BreamoreBoy commented on Sep 18, 2014

      BreamoreBoymannequin
      Mannequin

      How can anything that's in the stdlib be unified with something that's not in the stdlib and currently has no prospects of getting in the stdlib?

    9. serhiy-storchaka commented on Sep 19, 2014

      @serhiy-storchaka
      MemberAuthor

      The regex module is potential candidate for replacement of the re module.

    10. BreamoreBoy commented on Sep 19, 2014

      BreamoreBoymannequin
      Mannequin

      The key word is "potential". I do not believe that any changes should be made to the re module until such time as there is a fully approved PEP for the regex module and that work has actually started on getting it into the stdlib. Surely backward compatibility also comes into this?

    11. terryjreedy commented on Sep 19, 2014

      @terryjreedy
      Member

      Steven and Mark are correct that a tracker patch cannot change a 3rd party module. On the other hand, we are free to improve error messages in new versions. And we are free to borrow ideas from 3rd part modules. I changed the title accordingly.

      (Back compatibility comes into play in not making message enhancements in bugfix releases even though message details are not part of the documented API. People who write code that depends on those details, and doctexts need not so depend, should expect to revise for new versions. I expect that some of our re tests would need to be changed.)

      Re and regex are a bit special in that regex is the only re replacement (that I know of) and is (almost) a drop-in replacement. So some people *are*, on their own, replacing re with regex by installing regex (easy with pip) and adding 'import regex as re' at the top of their code.

      Serhiy suggested either picking the best or writing a new one, I think a new one combining both would be best in many of the cases. As a user, I like "name missing terminal '>'" for #2 (is there an adjective for a name in this context?) and for #4, "group name missing terminal '>'". (Note that we usually quote literals, as in #8.) For #12, I would like a parallel construction "set expression missing terminal ']'" if that is possible. But the currently vague re message "unexpected end of regular expression" might be raised as a point where the specific information is lost and only the general version is correct.

      As for #14, either UNICODE and LOCALE *are* compatible (for re) or this is buggy.
      >>> import re
      >>> re.compile(r'\w', re.UNICODE | re.LOCALE)
      re.compile('\\w', re.LOCALE|re.UNICODE)
    12. 6 remaining items

    13. rhettinger commented on Nov 2, 2014

      @rhettinger
      Contributor

      +1

    14. serhiy-storchaka commented on Nov 10, 2014

      @serhiy-storchaka
      MemberAuthor

      Here is a patch which makes re error messages match regex. It doesn't look to me that all these changes are enhancements.

    15. terryjreedy commented on Nov 12, 2014

      @terryjreedy
      Member

      I already said we should either stick with what we have if better (and gave examples, including sticking with 'cannot') or possibly combine the best of both if we can improve on both. 13 should use 'bytes-like' (already changed?). There is no review button.

    16. serhiy-storchaka commented on Feb 7, 2015

      @serhiy-storchaka
      MemberAuthor

      Here is a patch which unify and improves re error messages. Added tests for all parsing errors. Now error message always points on the start of affected component, i.e. on the start of bad escape, group name or unterminated subpattern.

    17. serhiy-storchaka commented on Feb 7, 2015

      @serhiy-storchaka
      MemberAuthor

      re_errors_diff.txt contains differences for all tested error messages.

    18. serhiy-storchaka commented on Feb 10, 2015

      @serhiy-storchaka
      MemberAuthor

      Updated patch addresses Ezio's comments.

    19. serhiy-storchaka commented on Feb 18, 2015

      @serhiy-storchaka
      MemberAuthor

      Here is a patch for regex which makes some error messages be the same as in re with re_errors_2.patch. You could apply it to regex if new error messages look better than old error messages. Otherwise we could change re error messages to match regex, or discuss better variants.

    20. mrabarnett commented on Feb 18, 2015

      mrabarnettmannequin
      Mannequin

      Some error messages use the indefinite article:

      "expected a bytes-like object, %.200s found"
      "cannot use a bytes pattern on a string-like object"
      "cannot use a string pattern on a bytes-like object"
      

      but others don't:

      "expected string instance, %.200s found"
      "expected str instance, %.200s found"
      

      Messages tend to be abbreviated, so I think that it would be better to just omit the article.

      I don't think that the error message "bad repeat interval" is an improvement (Why is it "bad"? What is an "interval"?). I think that saying that the min is greater than the max is clearer.

    21. serhiy-storchaka commented on Feb 20, 2015

      @serhiy-storchaka
      MemberAuthor

      Messages tend to be abbreviated, so I think that it would be better to just
      omit the article.

      I agree, but this is came from standard error messages which are not
      consistent. I opened a thread on Python-Dev.

      "expected a bytes-like object" and "expected str instance" are standard error
      messages raised in bytes.join and str.join, not in re. We could change them
      though.

      I don't think that the error message "bad repeat interval" is an improvement
      (Why is it "bad"? What is an "interval"?). I think that saying that the min
      is greater than the max is clearer.

      Agree. I'll change this in re. What message is better in case of overflow: "the
      repetition number is too large" (in re) or "repeat count too big" (in regex)?

    22. serhiy-storchaka commented on Feb 24, 2015

      @serhiy-storchaka
      MemberAuthor

      Updated patch borrows the error message about min > max from regex.

    23. serhiy-storchaka commented on Feb 24, 2015

      @serhiy-storchaka
      MemberAuthor

      Removed changing TypeError errors and "bad repeat interval" error in updated regex patch.

    24. serhiy-storchaka commented on Mar 1, 2015

      @serhiy-storchaka
      MemberAuthor

      Could anyone please make a review? This patch is a prerequisite of other patches.

    25. python-dev commented on Mar 25, 2015

      python-devmannequin
      Mannequin

      New changeset 068365acbe73 by Serhiy Storchaka in branch 'default':
      Issue bpo-22364: Improved some re error messages using regex for hints.
      https://hg.python.org/cpython/rev/068365acbe73

    26. 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

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-regextype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions