-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
gh-144694: Fix re.Match.group() doc claiming [1..99] range limit #144696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
The documentation incorrectly stated that numeric group arguments must be in the range [1..99]. This limit was removed in Python 3.5 (bpo-22437). Replace with "a positive integer" since the next sentence already documents the IndexError for out-of-range values.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. 👍
| [1..99], it is the string matching the corresponding parenthesized group. If a | ||
| group number is negative or larger than the number of groups defined in the | ||
| pattern, an :exc:`IndexError` exception is raised. If a group is contained in a | ||
| return value is the entire matching string; if it is a positive integer, it is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually there seems to be a limit and it can be quantized on one's machine:
>>> import _sre
>>> _sre.MAXGROUPS
1073741823
I see multiple guards in Lib/sre_parse.py that use the MAXGROUPS constant for the upper bound.
Maybe the docs should say in the inclusive range [1.. _sre.MAXGROUPS] or similar?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_sre.MAXGROUPS limits the number of groups allowed in a pattern. This is enforced at compile time in _parser.py.
The .group() method itself does not check against _sre.MAXGROUPS. It only checks whether the requested group index is within the number of groups defined in that specific pattern (see match_getindex, where it checks i >= self->groups).
The next sentence in the documentation already explains this clearly:
"If a group number is negative or larger than the number of groups defined in the pattern, an IndexError exception is raised."
Mentioning _sre.MAXGROUPS here would reference a private module and mix two different things:
- the compile-time limit on total groups in a pattern
- the runtime behavior of
.group().
So I think it is better not to mention _sre.MAXGROUPS in this context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are correct in making that distinction.
LGTM! 👍
The documentation for
re.Match.group()incorrectly states that numeric group arguments must be in the inclusive range[1..99]. This limit was removed in Python 3.5 (bpo-22437) when the number of capturing groups became dynamic.Replace
[1..99]with "a positive integer" since the next sentence already documents theIndexErrorfor out-of-range values.📚 Documentation preview 📚: https://cpython-previews--144696.org.readthedocs.build/