classification
Title: better name for re.error Exception class.
Type: enhancement Stage: patch review
Components: Regular Expressions Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, gregory.p.smith, mbussonn, mrabarnett, serhiy.storchaka, sourabh025
Priority: normal Keywords: easy, patch

Created on 2019-12-05 16:56 by mbussonn, last changed 2020-09-18 07:39 by serhiy.storchaka.

Pull Requests
URL Status Linked Edit
PR 17501 closed mbussonn, 2019-12-07 21:29
Messages (11)
msg357867 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2019-12-05 16:56
better error/exception name for re.compile error. 

Currently the error raise by re.compile when it fails to compile is `error` defined in sre_constants.py: 

```
class error(Exception):
    """Exception raised for invalid regular expressions.

```

This is quite disturbing as most exception start with an uppercase and have a tiny bit more descriptive name. 

Would it be possible to have it renamed as something more explicit like `ReCompileError`, and still keeping the potential `error` alias as deprecated ?
msg357878 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-12-05 18:26
It is common practice that the module specific exception is called just "error". There is nothing wrong with this.

I do not see a need to introduce a different alias.
msg357882 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-12-05 18:29
See also the discussion about renaming json.loads(): https://mail.python.org/archives/list/python-ideas@python.org/thread/EJTIVQ2ZFSVHALTLRGFCOMOYGZYMKGQU/
msg357883 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2019-12-05 18:34
Most of the module specific classes are `Error`, not `error`, at least with an uppercase E you know it's a class. 

if a novice sees :

> error: missing ), unterminated subpattern at position 0

It will be relatively tough or them to figure out that `error` is the type of the exception. 

Also it's not because something works that you can't improve it ...
msg357884 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-12-05 18:42
Since it affects more than one module I suggest to discuss the idea about renaming exceptions of the Python-Ideas maillist first. Until different decision be made I am closing. Personally I think this is a duplicate of just discussed and rejected idea.
msg357886 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2019-12-05 19:33
Thanks for the advice I've done that ! 

Have a good day.
msg357962 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-12-07 07:05
Reopened after discussing on Python-ideas: https://mail.python.org/archives/list/python-ideas@python.org/thread/64NHNY6RD4HQWBSBV6J7XIN7UAHNTQBR/.
msg357982 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2019-12-07 18:04
Thanks Serhiy, 

Here is a rough idea of how many places would be touched by renaming in the `re` module:

https://github.com/Carreau/cpython/commit/59e4c5150c842f849ff3a9ba8a94df1df7a5eb1c (50 additions and 42 deletions.). 

I haven't found any places that need changes in the C code, and need to polish documentation, rebuild and run the test suite before sending a PR.
msg357986 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2019-12-07 20:37
Strictly speaking not all of those _need_ to be touched given the old name is always going to exist for backwards compatibility.  But I agree that we should update them as part of this regardless.

I'd go forward with a PR.

The only fallout I expect a change like this to have on users is in the very odd test scenario where someone has hardcoded the error name in a string.  That is rare, especially for an re.error which is generally not an expected exception.
msg358059 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-12-09 07:32
I am not sure about the new name. "re" is an abbreviation, so if include it in the exception name it should be "RE". I am not sure what name is better: RECompileError, REParseError, RESyntaxError, REError, CompileError, ParseError, SyntaxError or Error.

json raises JSONDecodeError, ElementTree raises ParseError, other xml modules raise ExpatError, csv raises Error, configparser raises subclasses of Error.

Many modules (at least 18: aifc, binhex, concurrent.futures, configparser, copy, cvs, ftplib, locale, mailbox, shutil, sqlite, sunau, test.support, uu, wave, webbrowser, xdrlib, xmlrpc.client) have an exception named just Error for module-specific errors.
msg358124 - (view) Author: Matthias Bussonnier (mbussonn) * Date: 2019-12-09 18:33
> RECompileError, REParseError, RESyntaxError, REError, CompileError, ParseError, SyntaxError or Error, 

> Many modules [...] have an exception named just Error

RECompileError, REParseError, RESyntaxError, REError, CompileError, ParseError are all fine with me. 

SyntaxError would be super confusing IMHO.
Remember the StackTrace does not get the fully qualified name, so it would be hard to distinguish from a SyntaxError with the Python Syntax.

aifc, binhex, sunau, uu, xdrlib might be removed with PEP 594, And I find  `Error` not informative enough. It suffers from the same issues as above, as stack traces do not have the full qualified name. 

I would also add that being able to search and find all occurrences of a given exceptions is useful, and that Error is too generic.

Let me know your choice and I can rename.
History
Date User Action Args
2020-09-18 07:39:36serhiy.storchakasettype: security -> enhancement
2020-09-16 18:44:35sourabh025setnosy: + sourabh025
type: enhancement -> security
2019-12-09 18:33:10mbussonnsetmessages: + msg358124
2019-12-09 07:32:54serhiy.storchakasetmessages: + msg358059
2019-12-07 21:29:31mbussonnsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request16979
2019-12-07 20:37:37gregory.p.smithsetnosy: + gregory.p.smith
messages: + msg357986
2019-12-07 18:04:07mbussonnsetmessages: + msg357982
2019-12-07 09:32:23serhiy.storchakasetkeywords: + easy
2019-12-07 07:05:42serhiy.storchakasetstatus: closed -> open
versions: + Python 3.9
type: enhancement
messages: + msg357962

resolution: not a bug ->
stage: resolved -> needs patch
2019-12-05 19:33:33mbussonnsetmessages: + msg357886
2019-12-05 18:42:23serhiy.storchakasetstatus: open -> closed

messages: + msg357884
2019-12-05 18:34:05mbussonnsetstatus: closed -> open

messages: + msg357883
2019-12-05 18:29:41serhiy.storchakasetmessages: + msg357882
2019-12-05 18:26:10serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg357878

resolution: not a bug
stage: resolved
2019-12-05 16:56:07mbussonncreate