python-humanize / python-humanize/humanize
precisedelta() silently drops the sign of negative timedeltas
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 756
- Fork
- 148
- Merge medio
- 6g 1h
- PR unite (30g)
- 12
Descrizione
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)
- Preserve the sign: prefix the result with
-whenvalueis negative (mirroringstr(timedelta), which keeps the sign:str(td)→'-1 day, 1:01:01'). - 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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dall’implementazione di precisedelta(), dal suo docstring esistente e dai relativi test, quindi esegui la riproduzione fornita di negative-timedelta, incluso il caso minimum_unit="minutes". Aggiungi una copertura di regressione per i valori negativi e conferma che il comportamento scelto sia coerente, preservando l’output positivo esistente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Attiva
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 72/100