fix(chain): sort matches by match_index before groupby#49
Merged
Conversation
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>
Contributor
|
@Toilal I'm happy to help! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reworked, rebased and cleaned-up version of #31 (thanks @denini08 for the catch — credited as co-author).
Problem
itertools.groupbyonly groups consecutive equal keys. InChain._group_by_match_index, when matches arrive unordered, groups sharing amatch_indexget split and thegrouped_matches_dict[match_index] = list(match)assignment overwrites the earlier group, silently dropping matches.Fix
Sort by
match_indexbefore grouping — a no-op when the input is already ordered, and consistent with thesorted()-before-groupby()convention already used inrules.py.What changed vs #31
develop(the original no longer applied —chain.py/test_chain.pyhad diverged).sorted()exceeded the 120-char limit → ruff E501).-> None), no trailing whitespace, runtimeMatchimport. It fails without the fix (the twomatch_index=2matches collapse to one) and passes with it.Verification
mypy --strict: cleanpytest: 188 passed under both thereandregexbackendsruff/ fullpre-commit: cleanSupersedes #31.
🤖 Generated with Claude Code