fix(cleaners): avoid UnboundLocalError in _get_indexed_match when the pattern is absent - #4429
Open
eeshsaxena wants to merge 2 commits into
Open
fix(cleaners): avoid UnboundLocalError in _get_indexed_match when the pattern is absent#4429eeshsaxena wants to merge 2 commits into
eeshsaxena wants to merge 2 commits into
Conversation
…tern is absent When re.finditer yields no matches the for loop never runs, so the loop variable i is unbound and building the 'not found' message references it, raising UnboundLocalError instead of the intended ValueError. This is the normal pattern-absent path for extract_text_before / extract_text_after. Initialize i = -1 and only include 'The largest index was ...' when there was at least one match. Fixes Unstructured-IO#4427.
Contributor
There was a problem hiding this comment.
1 issue found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="unstructured/cleaners/extract.py">
<violation number="1" location="unstructured/cleaners/extract.py:21">
P3: The fix is correct, but it ships without a regression test for the exact crash it repairs — the PR's own repro `extract_text_before("hello world", "xyz")` would currently raise UnboundLocalError, and neither existing test in test_extract.py exercises the pattern-absent path. Please add a test asserting the pattern-absent case raises ValueError (not UnboundLocalError) and that the message omits the 'largest index' detail when there are no matches, so this fix doesn't silently regress.</violation>
</file>
Shadow auto-approve: would not auto-approve because issues were found.
Re-trigger cubic
| raise ValueError(f"The index is {index}. Index must be a non-negative integer.") | ||
|
|
||
| regex_match = None | ||
| i = -1 |
Contributor
There was a problem hiding this comment.
P3: The fix is correct, but it ships without a regression test for the exact crash it repairs — the PR's own repro extract_text_before("hello world", "xyz") would currently raise UnboundLocalError, and neither existing test in test_extract.py exercises the pattern-absent path. Please add a test asserting the pattern-absent case raises ValueError (not UnboundLocalError) and that the message omits the 'largest index' detail when there are no matches, so this fix doesn't silently regress.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At unstructured/cleaners/extract.py, line 21:
<comment>The fix is correct, but it ships without a regression test for the exact crash it repairs — the PR's own repro `extract_text_before("hello world", "xyz")` would currently raise UnboundLocalError, and neither existing test in test_extract.py exercises the pattern-absent path. Please add a test asserting the pattern-absent case raises ValueError (not UnboundLocalError) and that the message omits the 'largest index' detail when there are no matches, so this fix doesn't silently regress.</comment>
<file context>
@@ -18,12 +18,16 @@ def _get_indexed_match(text: str, pattern: str, index: int = 0) -> re.Match:
raise ValueError(f"The index is {index}. Index must be a non-negative integer.")
regex_match = None
+ i = -1
for i, result in enumerate(re.finditer(pattern, text)):
if i == index:
</file context>
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.
Fixes #4427.
Problem
_get_indexed_match(used byextract_text_before/extract_text_after) raisesUnboundLocalErrorinstead of a clear error when the pattern does not occur in the text:When
re.finditer(pattern, text)yields no matches, thefor i, result in ...loop never runs, soiis never bound. Theregex_match is Nonebranch then builds the message with{i}, raisingUnboundLocalErrorbefore the intendedValueError. This is the normal "pattern absent" path, so any caller ofextract_text_before/extract_text_afterwith a pattern that is not present hits it.Fix
Initialize
i = -1before the loop and only include the "largest index" detail when there was at least one match, so the pattern-absent case raises the intendedValueError.