Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion unstructured/cleaners/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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>

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}.")
message = f"Result with index {index} was not found."
if i >= 0:
message += f" The largest index was {i}."
raise ValueError(message)

return regex_match

Expand Down