Skip to content

fix(cleaners): avoid UnboundLocalError in _get_indexed_match when the pattern is absent - #4429

Open
eeshsaxena wants to merge 2 commits into
Unstructured-IO:mainfrom
eeshsaxena:fix-get-indexed-match-unbound
Open

fix(cleaners): avoid UnboundLocalError in _get_indexed_match when the pattern is absent#4429
eeshsaxena wants to merge 2 commits into
Unstructured-IO:mainfrom
eeshsaxena:fix-get-indexed-match-unbound

Conversation

@eeshsaxena

@eeshsaxena eeshsaxena commented Aug 8, 2026

Copy link
Copy Markdown

Fixes #4427.

Problem

_get_indexed_match (used by extract_text_before / extract_text_after) raises UnboundLocalError instead of a clear error when the pattern does not occur in the text:

extract_text_before("hello world", "xyz")  # UnboundLocalError: cannot access local variable 'i'

When re.finditer(pattern, text) yields no matches, the for i, result in ... loop never runs, so i is never bound. The regex_match is None branch then builds the message with {i}, raising UnboundLocalError before the intended ValueError. This is the normal "pattern absent" path, so any caller of extract_text_before / extract_text_after with a pattern that is not present hits it.

Fix

Initialize i = -1 before the loop and only include the "largest index" detail when there was at least one match, so the pattern-absent case raises the intended ValueError.

Review in cubic

…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.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

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.

extract_text_before/extract_text_after raise UnboundLocalError when the pattern is absent

1 participant