Skip to content

Commit b8a9df7

Browse files
anishnyajadchaar
andauthored
Fix floats appearing in Humanize output for locales that override _format_timeframe (#986)
* Fixed floats in humanize bug * Simplified changes * Fixed typo in comments and test cases Co-authored-by: Jad Chaar <jadchaar@users.noreply.github.com>
1 parent f1df58b commit b8a9df7

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

arrow/locales.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def describe(
147147
:param only_distance: return only distance eg: "11 seconds" without "in" or "ago" keywords
148148
"""
149149

150-
humanized = self._format_timeframe(timeframe, delta)
150+
humanized = self._format_timeframe(timeframe, trunc(delta))
151151
if not only_distance:
152152
humanized = self._format_relative(humanized, timeframe, delta)
153153

@@ -165,7 +165,8 @@ def describe_multi(
165165
"""
166166

167167
parts = [
168-
self._format_timeframe(timeframe, delta) for timeframe, delta in timeframes
168+
self._format_timeframe(timeframe, trunc(delta))
169+
for timeframe, delta in timeframes
169170
]
170171
if self.and_word:
171172
parts.insert(-1, self.and_word)
@@ -3318,7 +3319,7 @@ def _format_timeframe(
33183319
else:
33193320
key = timeframe
33203321

3321-
return self.timeframes[key].format(trunc(abs(delta)))
3322+
return self.timeframes[key].format(abs(delta))
33223323

33233324
def describe_multi(
33243325
self,
@@ -3334,7 +3335,7 @@ def describe_multi(
33343335

33353336
humanized = ""
33363337
for index, (timeframe, delta) in enumerate(timeframes):
3337-
last_humanized = self._format_timeframe(timeframe, delta)
3338+
last_humanized = self._format_timeframe(timeframe, trunc(delta))
33383339
if index == 0:
33393340
humanized = last_humanized
33403341
elif index == len(timeframes) - 1: # Must have at least 2 items

tests/test_arrow.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,25 @@ def test_untranslated_granularity(self, mocker):
22662266
with pytest.raises(ValueError):
22672267
arw.humanize(later, granularity="week")
22682268

2269+
# Bulgarian is an example of a language that overrides _format_timeframe
2270+
# Applicabale to all locales. Note: Contributors need to make sure
2271+
# that if they override describe or describe_mutli, that delta
2272+
# is truncated on call
2273+
2274+
def test_no_floats(self):
2275+
arw = arrow.Arrow(2013, 1, 1, 0, 0, 0)
2276+
later = arw.shift(seconds=55000)
2277+
humanize_string = arw.humanize(later, locale="bg", granularity="minute")
2278+
assert humanize_string == "916 минути назад"
2279+
2280+
def test_no_floats_multi_gran(self):
2281+
arw = arrow.Arrow(2013, 1, 1, 0, 0, 0)
2282+
later = arw.shift(seconds=55000)
2283+
humanize_string = arw.humanize(
2284+
later, locale="bg", granularity=["second", "minute"]
2285+
)
2286+
assert humanize_string == "916 минути 40 няколко секунди назад"
2287+
22692288

22702289
@pytest.mark.usefixtures("time_2013_01_01")
22712290
class TestArrowHumanizeTestsWithLocale:

0 commit comments

Comments
 (0)