search_dates() raises IndexError for the zh-Hans, zh-Hant and yue locales
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 88/100
Hướng nghiên cứu
Bắt đầu trong dateparser/languages/locale.py tại Locale.translate_search(), đặc biệt là nhánh nối từ quanh các dòng 302-312, sau đó kiểm tra luồng liên quan trong dateparser/search/search.py. Thêm kiểm thử hồi quy trong tests/test_search.py cho zh-Hans, zh-Hant và yue bằng các lệnh gọi được liệt kê, rồi chạy các kiểm thử đó cùng với toàn bộ test suite; hoàn thành khi không trường hợp nào phát sinh IndexError.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- Python
- Star
- 2.9k
- Fork
- 545
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 17
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của scrapinghub/dateparser
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
scrapinghub/dateparser#1617 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
StaticTzInfo has localize() but not normalize(), so pytz-detecting code crashes on itCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 70/100
scrapinghub/dateparser#1591 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Validate on Python 3.15Có thể đã có người làm @kishan3 đã nhận 25 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
scrapinghub/dateparser#1548 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Adopt sphinx-scrapy, with docs dependencies in a dependency groupCó thể đã có người làm @yamilmaud đã nhận 3 ngày trước. Đang mở
Độ khó 3/5 Nửa ngày Mức phù hợp với người mới 25/100
scrapinghub/dateparser#1613 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Enable branch coverage, upload test results and use Codecov OIDCCó thể đã có người làm @yamilmaud đã nhận 3 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 20/100
scrapinghub/dateparser#1611 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của scrapinghub/dateparser
Issue tương tự
-
[19.0] pos_sale: settling an order with a 0 down payment line crashes (reading 'reward_id') and leaves a wrong amount dueCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 79/100
developmentseed/titiler-covjson#164 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
awslabs/visual-asset-management-system#2164 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
MakazhanAlpamys/Soup#3589 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
channels status reports the wrong Enabled value for Feishu instances and default-enabled WebSocketĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
Maintainer thường phản hồi trong vòng 1 ngày