extract_text_before/extract_text_after raise UnboundLocalError when the pattern is absent
I maintainer di solito rispondono entro 1 giorno
@ayam04 ci sta già lavorando.
Dal 20/8/2026.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 78/100
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
- Include un Dockerfile o un file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Unstructured-IO/unstructured
-
fix(nlp): match single-letter bullets in the enumerated bullet patternForse già presa @Eapp1e l’ha presa 1 giorno fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
Unstructured-IO/unstructured#4635 ·
I maintainer di solito rispondono entro 1 giorno
-
CoordinateSystem cannot be compared or converted without a caller-set `orientation`Forse già presa @Eapp1e l’ha presa 4 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
Unstructured-IO/unstructured#4614 ·
I maintainer di solito rispondono entro 1 giorno
-
bug(cleaners): translate_text passes text instead of chunk in attention window loopForse già presa @gunjan-l l’ha presa 12 giorni fa. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
Unstructured-IO/unstructured#4558 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Large HTML documents cannot be partitioned using the partition_html function.Forse già presa @CrepuscularIRIS l’ha presa 197 giorni fa. Apertabug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
Unstructured-IO/unstructured#4289 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug/Dependency resolution discrepancy for numbaForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Unstructured-IO/unstructured#4216 · 4 commenti · 7 reazioni ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di Unstructured-IO/unstructured
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
BCDA-APS/polar-bits#93 ·
-
infer_audio_type only recognises one MPEG Layer III frame header, so most MP3 files are rejectedForse già presa @Steve-20-ai l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
processone/ejabberd#4649 ·
I maintainer di solito rispondono entro 4 giorni