From 12b43e0653186aef3ae21b29b1cde0d384af46a6 Mon Sep 17 00:00:00 2001 From: Maayan Matsliah Date: Wed, 13 May 2026 20:55:12 -0400 Subject: [PATCH 1/3] refactor: extract _SUPERSCRIPT_MAP constant and simplify scientific() --- src/humanize/number.py | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 77b79abe..094705b2 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -9,6 +9,20 @@ from .i18n import _ngettext_noop as NS_ from .i18n import _pgettext as P_ +_SUPERSCRIPT_MAP = { + "0": "⁰", + "1": "¹", + "2": "²", + "3": "³", + "4": "⁴", + "5": "⁵", + "6": "⁶", + "7": "⁷", + "8": "⁸", + "9": "⁹", + "-": "⁻", +} + TYPE_CHECKING = False if TYPE_CHECKING: from typing import TypeAlias @@ -415,19 +429,6 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: """ import math - exponents = { - "0": "⁰", - "1": "¹", - "2": "²", - "3": "³", - "4": "⁴", - "5": "⁵", - "6": "⁶", - "7": "⁷", - "8": "⁸", - "9": "⁹", - "-": "⁻", - } try: value = float(value) if not math.isfinite(value): @@ -442,13 +443,7 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: part2 = re.sub(r"^\+?(\-?)0*(.+)$", r"\1\2", part2) - new_part2 = [] - for char in part2: - new_part2.append(exponents[char]) - - final_str = part1 + " x 10" + "".join(new_part2) - - return final_str + return part1 + " x 10" + "".join(_SUPERSCRIPT_MAP[char] for char in part2) def clamp( From af13b7ee2d6c6c12d3a71b9b0dfcde2259338b26 Mon Sep 17 00:00:00 2001 From: Maayan Matsliah Date: Thu, 14 May 2026 11:21:44 -0400 Subject: [PATCH 2/3] move _SUPERSCRIPT_MAP and switch to list comprehension --- src/humanize/number.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 094705b2..9ad51e4b 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -9,6 +9,16 @@ from .i18n import _ngettext_noop as NS_ from .i18n import _pgettext as P_ + +TYPE_CHECKING = False +if TYPE_CHECKING: + from typing import TypeAlias + + # This type can be better defined by typing.SupportsFloat + # but that's a Python 3.8 only typing option. + NumberOrString: TypeAlias = float | str + + _SUPERSCRIPT_MAP = { "0": "⁰", "1": "¹", @@ -23,14 +33,6 @@ "-": "⁻", } -TYPE_CHECKING = False -if TYPE_CHECKING: - from typing import TypeAlias - - # This type can be better defined by typing.SupportsFloat - # but that's a Python 3.8 only typing option. - NumberOrString: TypeAlias = float | str - def _format_not_finite(value: float) -> str: """Utility function to handle infinite and nan cases.""" @@ -442,8 +444,8 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: import re part2 = re.sub(r"^\+?(\-?)0*(.+)$", r"\1\2", part2) + return part1 + " x 10" + "".join([_SUPERSCRIPT_MAP[char] for char in part2]) - return part1 + " x 10" + "".join(_SUPERSCRIPT_MAP[char] for char in part2) def clamp( From 13e54db8eada485ab2714e93c0445b37cda3db7e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 14 May 2026 16:06:39 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/humanize/number.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 9ad51e4b..f9033453 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -9,7 +9,6 @@ from .i18n import _ngettext_noop as NS_ from .i18n import _pgettext as P_ - TYPE_CHECKING = False if TYPE_CHECKING: from typing import TypeAlias @@ -447,7 +446,6 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: return part1 + " x 10" + "".join([_SUPERSCRIPT_MAP[char] for char in part2]) - def clamp( value: float, format: str = "{:}",