Skip to content

Commit 2e2a880

Browse files
fix: restore 3.10 timezone usage and revert ternaries
Use datetime.timezone.utc instead of datetime.UTC for Python 3.10 compatibility. Revert conditional-expression refactors per maintainer feedback while keeping zip(strict=True), Yoda fix, and list flattening. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 5ace689 commit 2e2a880

4 files changed

Lines changed: 22 additions & 9 deletions

File tree

src/humanize/filesize.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ def naturalsize(
8383
"""
8484
if gnu:
8585
suffix = suffixes["gnu"]
86+
elif binary:
87+
suffix = suffixes["binary"]
8688
else:
87-
suffix = suffixes["binary"] if binary else suffixes["decimal"]
89+
suffix = suffixes["decimal"]
8890

8991
base = 1024 if (gnu or binary) else 1000
9092
bytes_ = float(value)

src/humanize/number.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ def _format_not_finite(value: float) -> str:
5656

5757
if math.isnan(value):
5858
return "NaN"
59-
if math.isinf(value):
60-
return "+Inf" if value > 0 else "-Inf"
59+
if math.isinf(value) and value < 0:
60+
return "-Inf"
61+
if math.isinf(value) and value > 0:
62+
return "+Inf"
6163
return ""
6264

6365

@@ -154,15 +156,21 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
154156
value = value.replace(thousands_sep, "").replace(decimal_sep, ".")
155157
if not math.isfinite(float(value)):
156158
return _format_not_finite(float(value))
157-
value = float(value) if "." in value else int(value)
159+
if "." in value:
160+
value = float(value)
161+
else:
162+
value = int(value)
158163
else:
159164
if not math.isfinite(float(value)):
160165
return _format_not_finite(float(value))
161166
float(value)
162167
except (TypeError, ValueError):
163168
return str(value)
164169

165-
result = f"{value:,.{ndigits}f}" if ndigits is not None else f"{value:,}"
170+
if ndigits is not None:
171+
result = f"{value:,.{ndigits}f}"
172+
else:
173+
result = f"{value:,}"
166174
if thousands_sep != "," or decimal_sep != ".":
167175
result = result.translate(str.maketrans(",.", thousands_sep + decimal_sep))
168176
return result
@@ -553,6 +561,9 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str:
553561
else:
554562
ordinal_ = ""
555563
value_ = format(value, f".{digits}f")
556-
space = "" if not (unit or ordinal_) or unit in ("°", "′", "″") else " "
564+
if not (unit or ordinal_) or unit in ("°", "′", "″"):
565+
space = ""
566+
else:
567+
space = " "
557568

558569
return f"{value_}{space}{ordinal_}{unit}"

tests/test_i18n.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import humanize
1212

1313
with freeze_time("2020-02-02"):
14-
NOW = dt.datetime.now(tz=dt.UTC)
14+
NOW = dt.datetime.now(tz=dt.timezone.utc)
1515

1616

1717
@freeze_time("2020-02-02")

tests/test_time.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
with freeze_time(FROZEN_DATE):
2929
NOW = dt.datetime.now()
30-
NOW_UTC = dt.datetime.now(tz=dt.UTC)
30+
NOW_UTC = dt.datetime.now(tz=dt.timezone.utc)
3131
NOW_UTC_PLUS_01_00 = dt.datetime.now(tz=dt.timezone(offset=dt.timedelta(hours=1)))
3232
TODAY = dt.date.today()
3333
TOMORROW = TODAY + ONE_DAY_DELTA
@@ -303,7 +303,7 @@ def test_naturaldate(test_input: dt.date, expected: str) -> None:
303303
@freeze_time("2023-10-15 23:00:00+00:00")
304304
def test_naturaldate_tz_aware() -> None:
305305
"""naturaldate should compare dates in the timezone of the given value."""
306-
utc = dt.UTC
306+
utc = dt.timezone.utc
307307
aedt = dt.timezone(dt.timedelta(hours=11))
308308
cest = dt.timezone(dt.timedelta(hours=2))
309309
edt = dt.timezone(dt.timedelta(hours=-4))

0 commit comments

Comments
 (0)