This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Make the repr of re flags more readable
Type: enhancement Stage: resolved
Components: Library (Lib), Regular Expressions Versions: Python 3.8
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 2022-04-11 14:59 by admin. 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

The repr of inverted flags is even more verbose:

>>> ~(re.I|re.S|re.X)

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

>>> print(re.I)
>>> print(re.I|re.S|re.X)
>>> print(~re.I)
>>> print(~(re.I|re.S|re.X))

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)
>>> ~(re.I|re.S|re.X|(1<<10))
>>> print(re.I|re.S|re.X|(1<<10))
>>> print(~(re.I|re.S|re.X|(1<<10)))

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.I|re.S|re.X
>>> ~re.I
>>> ~(re.I|re.S|re.X)
>>> re.I|re.S|re.X|(1<<10)
>>> ~(re.I|re.S|re.X|(1<<10))

__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)
Date User Action Args
2022-04-11 14:59:13adminsetgithub: 80729
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