Skip to content

fix(cleaners): prevent UnboundLocalError in _get_indexed_match when pattern is absent - #4477

Open
Tyagiquamar wants to merge 1 commit into
Unstructured-IO:mainfrom
Tyagiquamar:fix/extract-indexed-match-unbound-local
Open

fix(cleaners): prevent UnboundLocalError in _get_indexed_match when pattern is absent#4477
Tyagiquamar wants to merge 1 commit into
Unstructured-IO:mainfrom
Tyagiquamar:fix/extract-indexed-match-unbound-local

Conversation

@Tyagiquamar

@Tyagiquamar Tyagiquamar commented Sep 6, 2026

Copy link
Copy Markdown

Closes #4427

Problem

When extract_text_before() or extract_text_after() was called with a pattern that had no matches in the input string, the loop in _get_indexed_match() never ran. As a result, the loop variable i was never bound, causing UnboundLocalError when building the ValueError message instead of raising the intended descriptive ValueError.

Solution

  1. Initialized largest_index = -1 before the iteration loop.
  2. In the error message construction when regex_match is None, check if largest_index < 0 and raise a clear ValueError("Result with index {index} was not found. No matches were found.").
  3. Added unit tests for zero-match cases across _get_indexed_match, extract_text_before, and extract_text_after in test_unstructured/cleaners/test_extract.py.

Review in cubic

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