Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

extract_text_before/extract_text_after raise UnboundLocalError when the pattern is absent

Aperta Adatta ai principianti
#4,427 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@ayam04 ci sta già lavorando.

Dal 20/8/2026.

  • #4429 di @eeshsaxena — chiusa senza merge
  • #4441 di @mayuriphad — chiusa senza merge
  • #4450 di @ayam04 — aperta
  • #4477 di @Tyagiquamar — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia in unstructured/cleaners/extract.py e ispeziona _get_indexed_match(); quindi esegui la riproduzione fornita di extract_text_before() con un pattern assente. Aggiungi una copertura di regressione per il percorso con zero corrispondenze e verifica che non esponga più UnboundLocalError, producendo invece il ValueError previsto o un risultato corretto senza corrispondenze.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
HTML
Stelle
15.7k
Fork
1.4k
Merge medio
3g 8h
PR unite (30g)
77

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Unstructured-IO/unstructured

Tutte le issue di Unstructured-IO/unstructured

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.