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
Improve some re error messages using regex for hints #66560
Comments
In some cases standard re module and third-party regex modules raise exceptions with different error messages.
re: Cannot process flags argument with a compiled pattern
re: unterminated name
re: unterminated name
re: unterminated group name
re: unterminated group name
re: bad character in group name
re: negative group number
re: bad character in backref group name '!'
re: missing group name
re: bogus escape (end of line)
re: bogus escape: '\1'
re: unexpected end of regular expression
re: expected bytes, bytearray, or an object with the buffer interface, str found
re: ASCII and UNICODE flags are incompatible
re: unbalanced parenthesis
re: unbalanced parenthesis
re: cannot refer to 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. |
re: multiple repeat |
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. |
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). |
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?
|
It's interesting question. Grepping in CPython sources got results: Cannot 210 Lowercase wins uppercase with score 4:1 and short and long forms are I left the decision to English speakers.
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. |
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. |
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? |
The regex module is potential candidate for replacement of the re module. |
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? |
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) |
On Fri, Sep 19, 2014 at 08:41:57PM +0000, Mark Lawrence wrote:
Why is this so controversial? We're not talking about functional changes |
+1 on the idea. |
+1 |
Here is a patch which makes re error messages match regex. It doesn't look to me that all these changes are enhancements. |
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. |
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. |
re_errors_diff.txt contains differences for all tested error messages. |
Updated patch addresses Ezio's comments. |
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. |
Some error messages use the indefinite article:
but others don't:
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. |
I agree, but this is came from standard error messages which are not "expected a bytes-like object" and "expected str instance" are standard error
Agree. I'll change this in re. What message is better in case of overflow: "the |
Updated patch borrows the error message about min > max from regex. |
Removed changing TypeError errors and "bad repeat interval" error in updated regex patch. |
Could anyone please make a review? This patch is a prerequisite of other patches. |
New changeset 068365acbe73 by Serhiy Storchaka in branch 'default': |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: