[New-bugs-announce] [issue36548] Make the repr of re flags more readable

Serhiy Storchaka report at bugs.python.org
Sun Apr 7 06:28:01 EDT 2019


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

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().

----------
components: Library (Lib), Regular Expressions
messages: 339567
nosy: ethan.furman, ezio.melotti, mrabarnett, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Make the repr of re flags more readable
type: enhancement
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36548>
_______________________________________


More information about the New-bugs-announce mailing list