classification
Title: Make the repr of re flags more readable
Type: enhancement Stage: resolved
Components: Library (Lib), Regular Expressions Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ethan.furman, ezio.melotti, mrabarnett, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-04-07 10:28 by serhiy.storchaka, last changed 2019-06-10 08:02 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 12715 merged serhiy.storchaka, 2019-04-07 10:31
Messages (2)
msg339567 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-04-07 10:28
Currently the repr of re flags contains the name of the private class and it is not an evaluable expression:

>>> re.I
<RegexFlag.IGNORECASE: 2>
>>> re.I|re.S|re.X
<RegexFlag.VERBOSE|DOTALL|IGNORECASE: 82>

The repr of inverted flags is even more verbose:

<RegexFlag.ASCII|DEBUG|VERBOSE|UNICODE|DOTALL|MULTILINE|LOCALE|TEMPLATE: -3>
>>> ~(re.I|re.S|re.X)
<RegexFlag.ASCII|DEBUG|UNICODE|MULTILINE|LOCALE|TEMPLATE: -83>

The result of str() starves from the same issues.

>>> print(re.I)
RegexFlag.IGNORECASE
>>> print(re.I|re.S|re.X)
RegexFlag.VERBOSE|DOTALL|IGNORECASE
>>> print(~re.I)
RegexFlag.ASCII|DEBUG|VERBOSE|UNICODE|DOTALL|MULTILINE|LOCALE|TEMPLATE
>>> print(~(re.I|re.S|re.X))
RegexFlag.ASCII|DEBUG|UNICODE|MULTILINE|LOCALE|TEMPLATE

If the value contains unrecognized flags, it looks even more weird, and this information is not shown for invered flags:

>>> re.I|re.S|re.X|(1<<10)
<RegexFlag.1024|VERBOSE|DOTALL|IGNORECASE: 1106>
>>> ~(re.I|re.S|re.X|(1<<10))
<RegexFlag.ASCII|DEBUG|UNICODE|MULTILINE|LOCALE|TEMPLATE: -1107>
>>> print(re.I|re.S|re.X|(1<<10))
RegexFlag.1024|VERBOSE|DOTALL|IGNORECASE
>>> print(~(re.I|re.S|re.X|(1<<10)))
RegexFlag.ASCII|DEBUG|UNICODE|MULTILINE|LOCALE|TEMPLATE

This repr is also not consistent with the represenation of flags in the repr of the compiled pattern:

>>> re.compile('x', re.I|re.S|re.X)
re.compile('x', re.IGNORECASE|re.DOTALL|re.VERBOSE)

The proposed PR makes the repr be the same as for flags in the repr of the compiled pattern and represents inverted flags as the result of inversion:

>>> re.I
re.IGNORECASE
>>> re.I|re.S|re.X
re.IGNORECASE|re.DOTALL|re.VERBOSE
>>> ~re.I
~re.IGNORECASE
>>> ~(re.I|re.S|re.X)
~(re.IGNORECASE|re.DOTALL|re.VERBOSE)
>>> re.I|re.S|re.X|(1<<10)
re.IGNORECASE|re.DOTALL|re.VERBOSE|0x400
>>> ~(re.I|re.S|re.X|(1<<10))
~(re.IGNORECASE|re.DOTALL|re.VERBOSE|0x400)

__str__ is set to object.__str__, so that str() will return the same as repr().
msg344031 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-05-31 07:39
New changeset 14a0e16c8805f7ba7c98132ead815dcfdf0e9d33 by Serhiy Storchaka in branch 'master':
bpo-36548: Improve the repr of re flags. (GH-12715)
https://github.com/python/cpython/commit/14a0e16c8805f7ba7c98132ead815dcfdf0e9d33
History
Date User Action Args
2019-06-10 08:02:04serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-05-31 07:39:53serhiy.storchakasetmessages: + msg344031
2019-04-07 10:31:22serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request12640
2019-04-07 10:28:01serhiy.storchakacreate