python / python/cpython

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

Abierto
#141,540 1 comentario 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza en Lib/_strptime.py alrededor del error de datos no convertidos en las líneas citadas en el informe y reproduce el ejemplo con el salto de línea final. El trabajo estará terminado cuando los datos restantes se muestren sin ambigüedad usando un formato de estilo repr, coherente con el mensaje de error cercano.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
backend
Tipo de issue
Error
Dificultad
1/5
Tiempo estimado
Menos de una hora
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.