fix: raise ValueError instead of UnboundLocalError in _get_indexed_match on zero matches - #4401
Open
chuenchen309 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
No issues found across 4 files
Shadow auto-approve: would auto-approve. Fixes a clear bug where zero matches raised UnboundLocalError instead of the intended ValueError; the change is a one-line initialization plus a regression test, bounded and clearly beneficial.
Re-trigger cubic
…tch on zero matches `for i, result in enumerate(re.finditer(pattern, text))` never binds `i` when the pattern matches zero times, so the "not found" branch's error message (which references `i`) raises UnboundLocalError instead of the intended ValueError -- breaking extract_text_before()'s and extract_text_after()'s documented "pattern not found" contract. Initialize `i = -1` before the loop so the existing ValueError branch can safely report a sensible "largest index" even with zero matches. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
chuenchen309
force-pushed
the
fix/get-indexed-match-unbound-local-error
branch
from
July 15, 2026 22:05
b5d6953 to
3e6064b
Compare
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.
Problem
_get_indexed_match(unstructured/cleaners/extract.py) doesfor i, result in enumerate(re.finditer(pattern, text)), then referencesiin the "not found" branch's error message. When the pattern matches zero times, the loop body never runs, soiis never bound — the intendedValueError("Result with index ... was not found...")instead raisesUnboundLocalError: cannot access local variable 'i', breakingextract_text_before()'s andextract_text_after()'s documented "pattern not found" contract.(The existing
test_get_indexed_match_raises_value_error_with_index_too_hightest doesn't catch this — it uses a pattern that does match, just fewer times than the requested index, soiwas always bound there.)Fix
Initialize
i = -1before the loop, so the existingValueErrorbranch can safely report a sensible "largest index" (-1, meaning none) even with zero matches.Testing
test_get_indexed_match_raises_value_error_with_zero_matches(a pattern with zero matches in the text) alongside the existing index-too-high test.extract.pyreproducesUnboundLocalError: cannot access local variable 'i'; reapplying passes.test_unstructured/cleaners/(140 tests) — all pass.ruff check— clean.unstructured/cleaners/extract.py.Note: this bumps
CHANGELOG.md/__version__.pyto0.25.1, matching the same version already proposed by two of my other open PRs (#4397, #4398) and one already-merged... actually still-open (#4399) against this same base — all four independently propose the same next-patch version bump since none have merged yet. Whichever merges first should "win" the bump; the rest will need a trivial rebase, which I'm happy to do once the ordering is known.AI-Generated disclosure
Found via an AI-assisted code review pass (Claude Code) over
unstructured/cleaners/. I personally reproduced theUnboundLocalError, verified the fix, and ran the relevant test suite plus lint before submitting.Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com