Title: Option for regex groupdict() to show only matching names
Type: enhancement Stage: patch review
Components: Regular Expressions Versions: Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, larry, mrabarnett, ned.deily, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2012-06-03 17:01 by rhettinger, last changed 2016-10-17 01:57 by mrabarnett.

File name Uploaded Description Edit
re_groupdict_no_default_future_warning.patch serhiy.storchaka, 2016-10-16 20:51 review
Messages (8)
msg162223 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-06-03 17:01
Currently, mo.groupdict() always inserts a default value for unmatched named groups.   This is helpful in some use cases and awkward in others.

I propose adding an option to suppress default entries:

>>> pattern = r'(?P<TITLE>Mrs |Mr |Dr )?(?P<LASTNAME>\w+)(?P<SUFFIX> Phd| JD)?'
>>> print re.match(pattern, 'Dr Who').groupdict()
{'LASTNAME': 'Who', 'SUFFIX': None, 'TITLE': 'Dr '}

>>> print re.match(pattern, 'Dr Who').groupdict(nodefault=True)
{'LASTNAME': 'Who', 'TITLE': 'Dr '}

>>> print re.match(pattern, 'Dr Who', re.NODEFAULT).groupdict()
{'LASTNAME': 'Who', 'TITLE': 'Dr '}

There is probably a better name than "nodefault", but I would like to see someway to improve the usability of groupdict().
msg163015 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-06-17 05:41
Just a thought--how about a new constant in the re module:
and have the interface be
msg163077 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2012-06-17 16:17
@rhettinger: The problem with "nodefault" is that it's negative, so that "nodefault=False" means that you don't not want the default, if you see what I mean. I think that "suppress" would be better:


@larry: If the parameter was "default", would that mean that you could provide a different default, such as:

msg163081 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-06-17 18:26
@mrabarnett: That's right--except your tense is wrong.  mo.groupdict() has supported the "default" parameter since the function was first added, way back in 1.5.2.
msg278756 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-16 09:14
Maybe start emitting FutureWarning when groupdict() is called without an argument? groupdict(None) returns the same result but without a warning. After 1 or 2 releases the behavior can be changed: groupdict() will return only matched groups.
msg278777 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-16 20:51
Proposed patch makes groupdict() without argument emitting FutureWarning if the result includes unmatched groups. Maybe warning message and the documentation could be better.

Ned, I ask your permission for including this change in 3.6. Yes, it can break third-party code that ran with -Werror, but I think groupdict() is rarely used in comparison of other match object API. The workaround is simple -- just use groupdict(None). The earlier we add a warning, the earlier we can change the behavior of groupdict(). If we did it in 3.4 (the time Raymond opened this issue), we could get new behavior in 3.7.
msg278789 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2016-10-17 00:09
How do others feel about Serhiy's proposal for eventually changing the semantics of groupdict() to mean only return matched groups?  If that seems reasonable, then there is the separate question of how to make the transition.  Adding a somewhat unpredictable FutureWarning, i.e. one that depends on the input, seems somewhat risky and user-unfriendly at this late stage of the release cycle.  But I'm open to persuasion.
msg278790 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2016-10-17 01:57
I'd prefer an explicit suppression to changing a long-standing behaviour.
Date User Action Args
2016-10-17 01:57:15mrabarnettsetmessages: + msg278790
2016-10-17 00:09:33ned.deilysetmessages: + msg278789
2016-10-16 20:51:20serhiy.storchakasetfiles: + re_groupdict_no_default_future_warning.patch

nosy: + ned.deily
messages: + msg278777

keywords: + patch
stage: needs patch -> patch review
2016-10-16 09:14:03serhiy.storchakasetnosy: + serhiy.storchaka

messages: + msg278756
versions: + Python 3.7, - Python 3.4
2012-06-17 18:26:34larrysetmessages: + msg163081
2012-06-17 16:17:36mrabarnettsetmessages: + msg163077
2012-06-17 05:41:37larrysetnosy: + larry
messages: + msg163015
2012-06-15 08:03:36ezio.melottisetnosy: + ezio.melotti, mrabarnett

components: + Regular Expressions
stage: needs patch
2012-06-03 17:01:00rhettingercreate