Repository navigation
Fix td_format rendering negative durations as large positive ones - #72703
CaptainAni187 wants to merge 3 commits into
Conversation
`td_format` converted days to months with `divmod`, which floors, so a relativedelta of days=-1 became months=-1, days=+29. `_format_part` then dropped the negative month because it skips any component below 1, leaving the 29 days behind. timedelta(seconds=-3752) rendered as '29d:22h:57M:28s'. The numeric branch disagreed with the timedelta branch for the same duration, since relativedelta(seconds=-3752) keeps a single negative seconds field rather than borrowing a day, so every component was dropped and the result fell through to '<1s'. Format the magnitude and re-apply the sign, so a negative duration reads as '-1h:2M:32s' and both input types agree.
|
Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
|
rjgoyln
left a comment
There was a problem hiding this comment.
LGTM overall! Two small readability points on the fix itself.
Just my thoughts — feel free to resolve them if you think otherwise.
| is_negative = td_object < dt.timedelta(0) if isinstance(td_object, dt.timedelta) else td_object < 0 | ||
| if isinstance(td_object, dt.timedelta): | ||
| delta = relativedelta() + td_object | ||
| delta = relativedelta() + abs(td_object) | ||
| else: | ||
| delta = relativedelta(seconds=int(td_object)) | ||
| delta = relativedelta(seconds=int(abs(td_object))) |
There was a problem hiding this comment.
The ternary re-runs the isinstance check that the very next if performs. Folding the sign decision into the existing branch keeps one type dispatch instead of two:
| is_negative = td_object < dt.timedelta(0) if isinstance(td_object, dt.timedelta) else td_object < 0 | |
| if isinstance(td_object, dt.timedelta): | |
| delta = relativedelta() + td_object | |
| delta = relativedelta() + abs(td_object) | |
| else: | |
| delta = relativedelta(seconds=int(td_object)) | |
| delta = relativedelta(seconds=int(abs(td_object))) | |
| if isinstance(td_object, dt.timedelta): | |
| is_negative = td_object < dt.timedelta(0) | |
| delta = relativedelta() + abs(td_object) | |
| else: | |
| is_negative = td_object < 0 | |
| delta = relativedelta(seconds=int(abs(td_object))) |
| A negative duration is formatted by magnitude and prefixed with `-`, | ||
| so timedelta(seconds=-3752) would become `-1h:2M:32s`. |
There was a problem hiding this comment.
The sign rule has one exception that the docstring doesn't mention: a magnitude below one second still renders as an unsigned <1s, so td_format(-0.4) == td_format(0.4) == "<1s". That's a reasonable choice — and the PR description explains it — but a reader of the docstring alone would expect a - here.
| A negative duration is formatted by magnitude and prefixed with `-`, | |
| so timedelta(seconds=-3752) would become `-1h:2M:32s`. | |
| A negative duration is formatted by magnitude and prefixed with `-`, | |
| so timedelta(seconds=-3752) would become `-1h:2M:32s`. A magnitude below | |
| one second is still `<1s`, without a sign. |
Addresses review: the ternary repeated the isinstance check that the next branch already performs. Also note the sub-second exception in the docstring, where a magnitude below one second stays unsigned.
|
Thanks @rjgoyln — both applied in 52db5de. You were right that the ternary was doing the same Good catch on the docstring too. The sub-second case was only explained in the PR description, which isn't much help to someone reading the function. It now says a magnitude below one second stays
|
|
Closing — this was fixed on main by #72774 (commit 3e8f918) while this PR was open, so the conflict here is my change against an equivalent one that already landed. Their approach negates up front with a Thanks @rjgoyln for the review earlier; the readability points you raised were good ones and I'd applied both. |
Closes: #72694
td_formatrenders a negative duration as a large positive one:The last two lines are the same duration expressed two ways and they disagree.
Cause
The day-to-month conversion uses
divmod, which floors:so a
relativedeltaofdays=-1becomesmonths=-1, days=+29._format_partskips any component below 1, which drops the-1 monththat would have cancelled the+29 daysand leaves the days behind.The numeric branch differs because
relativedelta(seconds=-3752)keeps a single negativesecondsfield instead of borrowing a day, so every component is dropped and the empty result falls through to<1s.Change
Format the magnitude, then re-apply the sign. This keeps the existing output for non-negative durations untouched and makes the two input types agree:
A magnitude below one second still reads
<1sregardless of sign, since prefixing a sign onto<1swould not tell the reader anything useful.Tests
Adds
test_td_format_negativealongside the existingtest_td_format, covering both input types, the sub-second case, and the agreement between the two branches. It fails onmainwith'29d:22h:57M:28s' != '-1h:2M:32s'.I also checked the sign invariant
td_format(-x) == "-" + td_format(x)over 20,000 randomly generated durations for both input types, along with cross-type agreement — no violations. That sweep is not included in the test file, since the explicit cases cover the behaviour and are easier to read.ruff checkandruff formatare clean at the pinned 0.16.4, and theshared/timezonessuite passes (31 passed).