Skip to content

fix(chain): sort matches by match_index before groupby#49

Merged
Toilal merged 1 commit into
developfrom
fix/chain-sort-before-groupby
Jun 28, 2026
Merged

fix(chain): sort matches by match_index before groupby#49
Toilal merged 1 commit into
developfrom
fix/chain-sort-before-groupby

Conversation

@Toilal

@Toilal Toilal commented Jun 28, 2026

Copy link
Copy Markdown
Owner

Reworked, rebased and cleaned-up version of #31 (thanks @denini08 for the catch — credited as co-author).

Problem

itertools.groupby only groups consecutive equal keys. In Chain._group_by_match_index, when matches arrive unordered, groups sharing a match_index get split and the grouped_matches_dict[match_index] = list(match) assignment overwrites the earlier group, silently dropping matches.

Fix

Sort by match_index before grouping — a no-op when the input is already ordered, and consistent with the sorted()-before-groupby() convention already used in rules.py.

sorted_matches = sorted(matches, key=lambda m: m.match_index)
for match_index, match in itertools.groupby(sorted_matches, lambda m: m.match_index):
    ...

What changed vs #31

  • Rebased on current develop (the original no longer applied — chain.py/test_chain.py had diverged).
  • Wrapped the line (the inline sorted() exceeded the 120-char limit → ruff E501).
  • Rewrote the test to pass the gates: typed (-> None), no trailing whitespace, runtime Match import. It fails without the fix (the two match_index=2 matches collapse to one) and passes with it.

Verification

  • mypy --strict: clean
  • pytest: 188 passed under both the re and regex backends
  • ruff / full pre-commit: clean
  • Confirmed the new test fails on the pre-fix code

Supersedes #31.

🤖 Generated with Claude Code

itertools.groupby only groups consecutive equal keys, so when matches
reach _group_by_match_index unordered, groups sharing a match_index get
split and the dict assignment overwrites earlier ones, silently dropping
matches. Sort by match_index first (no-op when already ordered), matching
the sorted()-before-groupby() convention already used in rules.py.

Reworked from #31 (rebased on develop, line wrapped, typed test that
fails without the fix).

Co-authored-by: denini08 <denini08@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Toilal
Toilal merged commit 11d77eb into develop Jun 28, 2026
23 checks passed
@Toilal
Toilal deleted the fix/chain-sort-before-groupby branch June 28, 2026 08:33
@Toilal Toilal mentioned this pull request Jun 28, 2026
@denini08

Copy link
Copy Markdown
Contributor

@Toilal I'm happy to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants