Unstructured-IO / Unstructured-IO/unstructured

extract_text_before/extract_text_after raise UnboundLocalError when the pattern is absent

Open Beginner friendly
#4,427 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
15.5k
Forks
1.3k
Avg merge
4d 2h
Merged PRs (30d)
13

Description

Summary

extract_text_before() and extract_text_after() (in unstructured/cleaners/extract.py) raise an UnboundLocalError instead of a clear error when the given pattern does not occur in the text at all. The helper they call, _get_indexed_match(), tries to build a "not found" message using a loop variable that was never assigned because the loop body never ran.

Reproduction
from unstructured.cleaners.extract import extract_text_before

# "xyz" does not appear in the text, so there are zero matches
extract_text_before("hello world", "xyz")
Actual
UnboundLocalError: cannot access local variable 'i' where it is not associated with a value
Expected

Either the intended ValueError ("Result with index 0 was not found.") or a graceful no-match result. The confusing UnboundLocalError should not surface.

Root cause

In _get_indexed_match:

for i, result in enumerate(re.finditer(pattern, text)):
    if i == index:
        regex_match = result

if regex_match is None:
    raise ValueError(f"Result with index {index} was not found. The largest index was {i}.")

When re.finditer(pattern, text) yields no matches, the for loop never executes, so i is never bound. The regex_match is None branch is then taken and the f-string references {i}, which raises UnboundLocalError before the intended ValueError can be raised. 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.

Suggested fix

Initialize the counter before the loop so the message is well-defined on zero matches, e.g.:

i = -1
for i, result in enumerate(re.finditer(pattern, text)):
    ...
if regex_match is None:
    raise ValueError(
        f"Result with index {index} was not found."
        + (f" The largest index was {i}." if i >= 0 else " No matches were found.")
    )

Happy to send a small PR with a regression test if that helps.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in unstructured/cleaners/extract.py and inspect _get_indexed_match(), then run the provided extract_text_before() reproduction with an absent pattern. Add regression coverage for the zero-match path and verify it no longer exposes UnboundLocalError, instead producing the intended ValueError or graceful no-match result.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.