Issue 36548: Make the repr of re flags more readable (original) (raw)

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