python / python/cpython

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

Aperta
#151,221 10 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in Lib/plistlib.py, in _date_from_string(), e rivedi il commento di _dateParser e il comportamento di matching per le date ISO 8601 parziali. Usa gli esempi nell'issue come casi di regressione, quindi controlla il PR #151217 collegato e l'altro lavoro collegato prima di procedere; il lavoro è completato quando i componenti omessi di mese, giorno e ora vengono caricati senza il TypeError segnalato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
data
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.