python-humanize / python-humanize/humanize

precisedelta() silently drops the sign of negative timedeltas

Abierto Apto para principiantes
#379 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
Python
Estrellas
756
Forks
148
Merge medio
6 d 1 h
PR fusionados (30 d)
12

Descripción

precisedelta() silently drops the sign of negative timedeltas — the output is byte-identical to the positive counterpart, so a negative delta rendered in any UI becomes indistinguishable from a positive one.

Version: humanize 4.16.0 / current master. Python 3.13.

Repro

import datetime as dt
from humanize import precisedelta

td = dt.timedelta(seconds=-3661)

print(precisedelta(td))                    # '1 hour, 1 minute and 1 second'
print(precisedelta(dt.timedelta(seconds=3661)))  # '1 hour, 1 minute and 1 second'  <- identical!

The sign is lost for every unit combination (also checked minimum_unit="minutes"). Neither the docstring nor the tests mention negative inputs at all, so it's unclear whether this is intended — but the current behavior makes the function unsafe for anything that can produce signed deltas (countdown corrections, progress deltas, A/B time differences), because the rendered string carries no trace of the direction.

For comparison, sibling function naturaldelta() also uses absolute value internally, but its output vocabulary ("a moment", "an hour") reads as inherently directionless; precisedelta's numeric output does not.

Suggested resolutions (either would work)

  1. Preserve the sign: prefix the result with - when value is negative (mirroring str(timedelta), which keeps the sign: str(td)'-1 day, 1:01:01').
  2. Or raise / document explicitly that only non-negative values are supported.

Option 1 seems most useful and backwards-compatible for positive inputs; option 2 at least removes the silent-corruption trap.

Happy to send a PR with either behavior + tests if maintainers indicate a preference.

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

Comienza por la implementación de precisedelta(), su docstring existente y sus pruebas; después ejecuta la reproducción proporcionada de negative-timedelta, incluido el caso minimum_unit="minutes". Añade cobertura de regresión para valores negativos y confirma que el comportamiento elegido es coherente, preservando la salida positiva existente.

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
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Activo
Claridad
Bastante claro
Aptitud para principiantes
72/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.