pypa/pip · error · IndexError

No such group

Error message

No such group

What it means

Raised by _PseudoMatch.group(arg) when a non-None group argument is supplied. _PseudoMatch is a stand-in match object (with a single implicit group 0) used internally when a token rule's action is a bare string rather than a real regex match. Asking it for a numbered/named group that doesn't exist raises IndexError.

Source

Thrown at src/pip/_vendor/pygments/lexer.py:377

class _PseudoMatch:
    """
    A pseudo match object constructed from a string.
    """

    def __init__(self, start, text):
        self._text = text
        self._start = start

    def start(self, arg=None):
        return self._start

    def end(self, arg=None):
        return self._start + len(self._text)

    def group(self, arg=None):
        if arg:
            raise IndexError('No such group')
        return self._text

    def groups(self):
        return (self._text,)

    def groupdict(self):
        return {}


def bygroups(*args):
    """
    Callback that yields multiple actions for each group in the match.
    """
    def callback(lexer, match, ctx=None):
        for i, action in enumerate(args):
            if action is None:
                continue
            elif type(action) is _TokenType:

View on GitHub (pinned to d7d0d0a394)

Solutions

  1. Ensure the regex in the token rule has one capture group per action passed to bygroups.
  2. Avoid calling match.group(n>0) inside callbacks attached to string/default rules.
  3. Test the rule's regex with re.compile and inspect .groups to confirm group count.

Example fix

# before: bygroups expects 2 groups but regex has 1
('([A-Za-z]+)', bygroups(Name, Name))
# after: add the second capture group
('([A-Za-z]+)([A-Za-z]+)', bygroups(Name, Name))
Defensive patterns

Strategy: validation

Validate before calling

import re
def groups_match(regex_str, n_actions):
    return re.compile(regex_str).groups >= n_actions

Try / catch

try:
    lexer.add_filter(my_callback_filter)
except IndexError:
    # rule/group mismatch; revise token definitions
    pass

Prevention

When it happens

Trigger: A custom lexer rule triggers bygroups or a callback that calls match.group(n) for n>=1, but the rule produced a _PseudoMatch (string action) instead of a real compiled-regex match with capture groups.

Common situations: Writing a RegexLexer rule whose bygroups arity exceeds the number of capture groups in the regex; mixing default(...) string token transitions with group access; porting a lexer rule that relied on groups that were later removed.

Related errors


AI-assisted analysis of pypa/pip@d7d0d0a394 (2026-08-04). Data as JSON: /data/errors/71ff35bc56044773.json. Report an issue: GitHub.