search_dates() raises IndexError for the zh-Hans, zh-Hant and yue locales
Los mantenedores suelen responder en 1 día
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 88/100
Línea de trabajo
Comienza en dateparser/languages/locale.py, en Locale.translate_search(), especialmente en la rama de unión de palabras alrededor de las líneas 302-312, y después inspecciona el flujo relacionado en dateparser/search/search.py. Añade cobertura de regresión en tests/test_search.py para zh-Hans, zh-Hant y yue usando las llamadas indicadas, y ejecuta esas pruebas junto con la suite completa; el trabajo estará terminado cuando ninguno de los casos genere IndexError.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Problem
search_dates() raises IndexError: list index out of range for the zh-Hans, zh-Hant and yue locales. It is reachable through the plainest possible call, with no languages= argument at all:
>>> from dateparser.search import search_dates
>>> search_dates("聽日") # Cantonese for "tomorrow"; autodetection selects yue
IndexError: list index out of range
>>> search_dates("舊年") # "last year"
IndexError: list index out of range
It also fires whenever one of the three locales is requested explicitly, including in a mixed language list, and including for plain ASCII input:
>>> search_dates("今天", languages=["zh-Hans"]) # IndexError
>>> search_dates("2020年3月4日", languages=["zh-Hant"]) # IndexError
>>> search_dates("00Z", languages=["yue"]) # IndexError
>>> search_dates("2020年3月4日", languages=["zh-Hans", "en"]) # IndexError
>>> search_dates("3 hours ago at 5pm", languages=["zh-Hans"]) # IndexError
zh, ja and every other locale are unaffected, and dateparser.parse() does not crash (for these locales it returns None instead — that half is #1277).
Traceback:
dateparser/search/__init__.py:70 -> search/search.py:357
-> search/search.py:184
-> search/search.py:45
-> languages/locale.py:310
[original_tokens[i], original_tokens[i + 1]],
IndexError: list index out of range
Root cause
Two things combine in Locale.translate_search() (dateparser/languages/locale.py):
locale.py:277—word_joint_unsupported_languages = ["zh", "ja"]. Exactlyzhandjacarry asimplificationsblock that rewrites年/月/日into separators before this point;zh-Hans,zh-Hantandyuedo not, so they reach the word-joining branch atlocale.py:302-312with raw CJK tokens, which the other two never do.locale.py:291computesnext_worddefensively —simplified_tokens[i + 1] if i < last_token_index else ""— but the branch it guards readsoriginal_tokens[i + 1]atlocale.py:310with no equivalent bounds check. On the final token,current_and_next_joinedcan still match a dictionary entry (the joined form equals the bare word, sincenext_wordis""), the branch is taken, and the index overflows.
Introduced in c5f0fa4 (#953, 2021-08-09); git show c5f0fa4^:dateparser/languages/locale.py contains no i + 1 index at all.
Proposal
Add the missing bounds check to the condition at locale.py:302:
elif (
i < last_token_index
and current_and_next_joined in dictionary
and word not in dashes
and self.shortname not in word_joint_unsupported_languages
):
I verified this locally:
- every repro above stops raising;
- the full suite is unchanged —
24186 passed, 18 skipped, 1 xfailed, identical to baseline; search_dates("聽日")andsearch_dates("舊年")now return correct results rather than merely not crashing.
Scope and considerations
- Please do not fix this by adding the three locales to
word_joint_unsupported_languages. I tried that: it stops the crash, butsearch_dates("2020年3月4日", languages=["zh-Hans"])then returnsdatetime(6, 5, 27)— silent garbage — becausetranslate_searchyields"2020yearmarch4day". That trades a loud crash for a wrong date. - Related but distinct: #1277 (
zh-Hansvszhlocale data) and #875 (zh-Hant). Those are data gaps that makeparse()returnNone; this is a code defect that survives them. I applied #1277's proposed change in-memory andsearch_dates()still raises for今天,昨天,上个月and00Z. Sensible sequencing is the bounds guard first (crash →None, matchingparse()), the data fix second (None→ correct date). - After the guard lands,
2020年3月4日underzh-HansreturnsNonerather than a date. That is #1277's territory, not a regression from this change.
Acceptance criteria
- None of the calls listed above raises
IndexError. - Regression tests cover
zh-Hans,zh-Hantandyueintests/test_search.py, which currently contains zero references to any of the three (grep -c 'zh-Han\|yue' tests/test_search.py→0) — which is how this survived four and a half years.
- Lenguaje dominante
- Python
- Estrellas
- 2.9k
- Forks
- 545
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 17
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de scrapinghub/dateparser
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
scrapinghub/dateparser#1617 ·
Los mantenedores suelen responder en 1 día
-
StaticTzInfo has localize() but not normalize(), so pytz-detecting code crashes on itQuizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. Abierto
Dificultad 1/5 1-3 horas Aptitud para principiantes 70/100
scrapinghub/dateparser#1591 ·
Los mantenedores suelen responder en 1 día
-
Validate on Python 3.15Posiblemente ocupada @kishan3 la tomó hace 25 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
scrapinghub/dateparser#1548 ·
Los mantenedores suelen responder en 1 día
-
Adopt sphinx-scrapy, with docs dependencies in a dependency groupPosiblemente ocupada @yamilmaud la tomó hace 3 días. Abierto
Dificultad 3/5 Medio día Aptitud para principiantes 25/100
scrapinghub/dateparser#1613 ·
Los mantenedores suelen responder en 1 día
-
Enable branch coverage, upload test results and use Codecov OIDCPosiblemente ocupada @yamilmaud la tomó hace 3 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 20/100
scrapinghub/dateparser#1611 ·
Los mantenedores suelen responder en 1 día
Todos los issues de scrapinghub/dateparser
Issues similares
-
[19.0] pos_sale: settling an order with a 0 down payment line crashes (reading 'reward_id') and leaves a wrong amount duePosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 79/100
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 79/100
developmentseed/titiler-covjson#164 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
awslabs/visual-asset-management-system#2164 ·
Los mantenedores suelen responder en 1 día
-
needs-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
MakazhanAlpamys/Soup#3589 ·
Los mantenedores suelen responder en 1 día
-
channels status reports the wrong Enabled value for Feishu instances and default-enabled WebSocketAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
Los mantenedores suelen responder en 1 día