precisedelta() silently drops the sign of negative timedeltas

Offen Anfängerfreundlich
#379 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
72/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
python
Bereich
backend

Rechercherichtung

Beginne bei der Implementierung von precisedelta() und ihrem vorhandenen Docstring und ihren Tests, führe dann die bereitgestellte negative-timedelta-Reproduktion einschließlich des Falls minimum_unit="minutes" aus. Füge Regressionstests für negative Werte hinzu und bestätige, dass das gewählte Verhalten konsistent ist, wobei die bestehende positive Ausgabe erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Python
Sterne
757
Forks
148
Ø Merge
6 T. 1 Std.
Gemergte PRs (30 T.)
12

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python-humanize/humanize

Alle Issues in python-humanize/humanize

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.