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.
Created on 2012年06月03日 17:01 by rhettinger, last changed 2022年04月11日 14:57 by admin.
| Files | ||||
|---|---|---|---|---|
| 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:
>>> # CURRENT WAY
>>> 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 '}
>>> # PROPOSED WAY
>>> print re.match(pattern, 'Dr Who').groupdict(nodefault=True)
{'LASTNAME': 'Who', 'TITLE': 'Dr '}
>>> # UPSTREAM VARIANT
>>> 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: SUPPRESSED=object() and have the interface be mo.groupdict(default=re.SUPPRESSED) |
|||
| 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: mo.groupdict(suppress=True) @larry: If the parameter was "default", would that mean that you could provide a different default, such as: mo.groupdict(default="") |
|||
| 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. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:31 | admin | set | github: 59196 |
| 2016年10月17日 01:57:15 | mrabarnett | set | messages: + msg278790 |
| 2016年10月17日 00:09:33 | ned.deily | set | messages: + msg278789 |
| 2016年10月16日 20:51:20 | serhiy.storchaka | set | files:
+ re_groupdict_no_default_future_warning.patch nosy: + ned.deily messages: + msg278777 keywords: + patch stage: needs patch -> patch review |
| 2016年10月16日 09:14:03 | serhiy.storchaka | set | nosy:
+ serhiy.storchaka messages: + msg278756 versions: + Python 3.7, - Python 3.4 |
| 2012年06月17日 18:26:34 | larry | set | messages: + msg163081 |
| 2012年06月17日 16:17:36 | mrabarnett | set | messages: + msg163077 |
| 2012年06月17日 05:41:37 | larry | set | nosy:
+ larry messages: + msg163015 |
| 2012年06月15日 08:03:36 | ezio.melotti | set | nosy:
+ ezio.melotti, mrabarnett components: + Regular Expressions stage: needs patch |
| 2012年06月03日 17:01:00 | rhettinger | create | |