Skip to content

fix(cleaners): raise clear ValueError when extract pattern is absent - #4450

Open
ayam04 wants to merge 1 commit into
Unstructured-IO:mainfrom
ayam04:fix/extract-no-match-error
Open

fix(cleaners): raise clear ValueError when extract pattern is absent#4450
ayam04 wants to merge 1 commit into
Unstructured-IO:mainfrom
ayam04:fix/extract-no-match-error

Conversation

@ayam04

@ayam04 ayam04 commented Aug 20, 2026

Copy link
Copy Markdown

Fixes #4427

_get_indexed_match referenced the enumerate loop variable i inside the "not found" error message, but i is never bound when re.finditer yields zero matches. So extract_text_before / extract_text_after raised an UnboundLocalError (crash) instead of the intended ValueError when the pattern does not occur in the text.

Track the largest match index in a dedicated variable initialized to -1, so the no-match path always raises a clear message: "Result with index 0 was not found. The largest index was -1."

Tests added:

  • test_get_indexed_match_no_match_raises_clear_error
  • test_extract_text_before_no_match_raises_clear_error

Validation: python3 -m pytest test_unstructured/cleaners/test_extract.py -q (41 passed), uv run ruff check clean.

Review in cubic

_get_indexed_match referenced the enumerate loop variable i in the
not-found error message, but i is never bound when re.finditer yields
zero matches, so extract_text_before/extract_text_after raised an
UnboundLocalError instead of the intended ValueError. Track the largest
match index explicitly so the no-match path always raises a clear
message.

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

No issues found across 2 files

Shadow auto-approve: would auto-approve. auto-approve - clear bug fix with focused diff and relevant tests

Re-trigger cubic

@ayam04

ayam04 commented Aug 23, 2026

Copy link
Copy Markdown
Author

Hi @cragwolfe @badGarnet — quick ping on this one: Fixes #4427 (extract_text_before/after raised UnboundLocalError when the pattern is absent; now raises a clear ValueError with the largest-index context, plus regression tests). Cubic review passed with 'No issues found'. Would appreciate a look when you have time.

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