`plstlib`: Raise `ValueError` instead of `TypeError` when loading a partial ISO 8601 date
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 35.9k
- Métriques de merge des PR
- Métriques de PR en attente
Description
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
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez dans Lib/plistlib.py, au niveau de _date_from_string(), et examinez le commentaire de _dateParser ainsi que le comportement de correspondance pour les dates ISO 8601 partielles. Utilisez les exemples de l’issue comme cas de régression, puis vérifiez le PR #151217 lié ainsi que les autres travaux liés avant de poursuivre ; le travail est terminé lorsque les composants mois, jour et heure omis sont chargés sans le TypeError signalé.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- data
- Type d'issue
- Bug
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Activité
- À l'abandon
- Clarté
- Clairement spécifiée
- Accessibilité débutants
- 25/100