python / python/cpython

`plstlib`: Raise `ValueError` instead of `TypeError` when loading a partial ISO 8601 date

Đang mở
#151,221 10 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

Bug report

plistlib is documented (in the code) to accept ISO 8601 <date> values where the smaller units are omitted: the date regex _dateParser makes the month, day and time components optional, and the comment above it says "Smaller units may be omitted with a loss of precision".

However, when a <date> value actually omits the day or month, loading fails with a confusing internal TypeError instead of producing a datetime:

>>> import plistlib
>>> plistlib.loads(b'<plist><date>2024-06Z</date></plist>')
Traceback (most recent call last):
  ...
TypeError: datetime() missing required argument 'day' (pos 3)
>>> plistlib.loads(b'<plist><date>2024Z</date></plist>')
Traceback (most recent call last):
  ...
TypeError: datetime() missing required argument 'month' (pos 2)

Dates that omit only the time components work fine:

>>> plistlib.loads(b'<plist><date>2024-06-07Z</date></plist>')
datetime.datetime(2024, 6, 7, 0, 0)
>>> plistlib.loads(b'<plist><date>2024-06-07T08Z</date></plist>')
datetime.datetime(2024, 6, 7, 8, 0)

The cause is in Lib/plistlib.py, _date_from_string(): it builds the positional argument list for datetime.datetime() by stopping at the first missing component, so for 2024-06Z it only passes (2024, 6) — fewer than the three required arguments (year, month, day).

The omitted components should default to the start of the period (month=1, day=1, hour/minute/second=0), consistent with how omitted time components already behave and with the documented "loss of precision".

I have a fix with a regression test ready (#151217).

CPython versions tested on:

3.16, CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-151217
  • gh-151285
  • gh-152958
  • gh-153244

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong Lib/plistlib.py tại _date_from_string() và xem lại chú thích của _dateParser cũng như hành vi khớp đối với các ngày ISO 8601 không đầy đủ. Sử dụng các ví dụ trong issue làm các trường hợp hồi quy, sau đó kiểm tra PR #151217 được liên kết và các công việc được liên kết khác trước khi tiếp tục; được xem là hoàn tất khi các thành phần tháng, ngày và thời gian bị lược bỏ được tải mà không gặp TypeError đã báo cáo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
data
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.