Skip to content

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

Open
mayuriphad wants to merge 1 commit into
Unstructured-IO:mainfrom
mayuriphad:fix/extract-match-absent-pattern
Open

fix(cleaners): prevent UnboundLocalError in _get_indexed_match when pattern is absent#4441
mayuriphad wants to merge 1 commit into
Unstructured-IO:mainfrom
mayuriphad:fix/extract-match-absent-pattern

Conversation

@mayuriphad

@mayuriphad mayuriphad commented Aug 18, 2026

Copy link
Copy Markdown

Summary

Closes #4427

extract_text_before() and extract_text_after() raised an UnboundLocalError instead of a ValueError when the target pattern was not present in the input text. The helper _get_indexed_match() referenced loop counter i in its formatted error message, which remained unassigned when re.finditer produced 0 matches.

Solution

  • Initialized i = -1 before the match iteration loop.
  • Added a clear error message Result with index {index} was not found. No matches were found. when no matches are detected.
  • Added unit regression tests in test_unstructured/cleaners/test_extract.py.

Review in cubic

Copilot AI lite review requested due to automatic review settings August 18, 2026 14:25

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@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. Focused bug fix preventing UnboundLocalError by initializing loop counter and raising a clear ValueError when no matches exist; regression tests pin the corrected behavior for both the helper and public functions.

Re-trigger cubic

@mayuriphad

Copy link
Copy Markdown
Author

Hi maintainers — just checking in on this PR since it hasn't had a maintainer review yet. Happy to make any changes needed; let me know if anything's unclear or if I should split/rework it. Thanks for your 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

2 participants