python / python/cpython

strptime: use %r in error messages, not %s

Aperta
#141,540 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug report

Bug description:

Consider the following example, say I'm parsing a date string read from an external source, and I forgot to call .strip():

from datetime import datetime

datetime.strptime("2001-02-03\n", "%Y-%m-%d")

The output is a little confusing:

$ python3 strptime_example.py
Traceback (most recent call last):
  File "strptime_example.py", line 3, in <module>
    datetime.strptime("2025-11-14\n", "%Y-%m-%d")
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_strptime.py", line 789, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/_strptime.py", line 558, in _strptime
    raise ValueError("unconverted data remains: %s" %
                      data_string[found.end():])
ValueError: unconverted data remains: 

The error message is using %s, when using %r would give a more unambiguous description of the extra data – similar with strings quoted manually using ' in the error message just above:

https://github.com/python/cpython/blob/a486d452c78a7dfcd42561f6c151bf1fef0a756e/Lib/_strptime.py#L567-L570

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Linked PRs
  • gh-141541
  • gh-141712

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/_strptime.py intorno all’errore relativo ai dati non convertiti alle righe citate nel report e riproduci l’esempio con il carattere di nuova riga finale. Il lavoro è completo quando i dati rimanenti vengono mostrati senza ambiguità usando una formattazione in stile repr, coerente con il messaggio di errore vicino.

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

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
1/5
Tempo stimato
Meno di un'ora
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.