python / python/cpython

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

未关闭
#151,221 10 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Lib/plistlib.py 中的 _date_from_string() 开始,检查 _dateParser 注释以及对不完整 ISO 8601 日期的匹配行为。使用 issue 中的示例作为回归用例,然后在继续之前检查关联的 PR #151217 和其他关联的工作;当省略的月份、日期和时间组件能够加载且不出现报告的 TypeError 时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
data
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。