From 6d2dcf6bbb66fe9bb1abad1d4ac46a4688d7f0ed Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Wed, 5 Oct 2022 21:38:37 +0530 Subject: [PATCH 01/18] Bug-Fix: handle NaN --- src/humanize/number.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index bfd2b48..30914d5 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -516,7 +516,12 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str: Returns: str: """ - exponent = int(math.floor(math.log10(abs(value)))) if value != 0 else 0 + if math.isnan(value): + return "NaN" + elif value != 0: + exponent = int(math.floor(math.log10(abs(value)))) + else: + exponent = 0 if exponent >= 27 or exponent < -24: return scientific(value, precision - 1) + unit From 414fe735ed08224ab2749b285f8a44695c20b4c1 Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Thu, 6 Oct 2022 01:08:39 +0530 Subject: [PATCH 02/18] handle NaN and Inf --- src/humanize/number.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 30914d5..f711063 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -518,10 +518,9 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str: """ if math.isnan(value): return "NaN" - elif value != 0: - exponent = int(math.floor(math.log10(abs(value)))) - else: - exponent = 0 + if not math.isfinite(value): + return "Inf" + exponent = int(math.floor(math.log10(abs(value)))) if value != 0 else 0 if exponent >= 27 or exponent < -24: return scientific(value, precision - 1) + unit From 69dcb60546446c4a637d5d59754ef922c80dc991 Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Thu, 6 Oct 2022 01:18:24 +0530 Subject: [PATCH 03/18] handle inf --- src/humanize/number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index f711063..3a939fc 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -518,7 +518,7 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str: """ if math.isnan(value): return "NaN" - if not math.isfinite(value): + if math.isinf(value): return "Inf" exponent = int(math.floor(math.log10(abs(value)))) if value != 0 else 0 From b4dbe7016be63809a87cff546b5ce93c61ab6f3d Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Thu, 6 Oct 2022 09:43:13 +0530 Subject: [PATCH 04/18] -inf and +inf --- src/humanize/number.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 3a939fc..70baf80 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -518,8 +518,10 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str: """ if math.isnan(value): return "NaN" - if math.isinf(value): - return "Inf" + if math.isinf(value) and value < 0: + return "-Inf" + elif math.isinf(value) and value > 0: + return "+Inf" exponent = int(math.floor(math.log10(abs(value)))) if value != 0 else 0 if exponent >= 27 or exponent < -24: From f70164403d7545520f4ceeaaf260a3fe9907c94d Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Thu, 6 Oct 2022 19:11:06 +0530 Subject: [PATCH 05/18] Add tests for inf and nan --- tests/test_number.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_number.py b/tests/test_number.py index b429931..900113e 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -5,6 +5,7 @@ import pytest +import math import humanize from humanize import number @@ -226,6 +227,10 @@ def test_clamp(test_args: list[typing.Any], expected: str) -> None: ([0.1, "°"], "100m°"), ([100], "100"), ([0.1], "100 m"), + ([math.nan], "NaN"), + ([math.nan, "m"], "NaN"), + ([math.inf], "+Inf"), + ([-math.inf], "-Inf"), ], ids=str, ) From c8b859b351738565c7beb054765f6e81432dd595 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 6 Oct 2022 13:41:36 +0000 Subject: [PATCH 06/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_number.py b/tests/test_number.py index 900113e..7a77daa 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -1,11 +1,11 @@ """Number tests.""" from __future__ import annotations +import math import typing import pytest -import math import humanize from humanize import number From 3c61efdb302c39021f12067952a3c52362215f35 Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Sun, 9 Oct 2022 18:18:45 +0530 Subject: [PATCH 07/18] Update number.py --- src/humanize/number.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 70baf80..15adcb9 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -64,7 +64,7 @@ def ordinal(value: NumberOrString, gender: str = "male") -> str: """ try: value = int(value) - except (TypeError, ValueError): + except (TypeError, ValueError, OverflowError): return str(value) if gender == "male": t = ( @@ -209,7 +209,7 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str: """ try: value = int(value) - except (TypeError, ValueError): + except (TypeError, ValueError, OverflowError): return str(value) if value < 0: @@ -272,7 +272,7 @@ def apnumber(value: NumberOrString) -> str: """ try: value = int(value) - except (TypeError, ValueError): + except (TypeError, ValueError, OverflowError): return str(value) if not 0 <= value < 10: return str(value) @@ -328,6 +328,12 @@ def fractional(value: NumberOrString) -> str: Returns: str: Fractional number as a string. """ + if math.isnan(value): + return "nan" + if math.isinf(value) and value < 0: + return "-inf" + elif math.isinf(value) and value > 0: + return "+inf" try: number = float(value) except (TypeError, ValueError): @@ -378,6 +384,12 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: Returns: str: Number in scientific notation z.wq x 10ⁿ. """ + if math.isnan(value): + return "nan" + if math.isinf(value) and value < 0: + return "-inf" + elif math.isinf(value) and value > 0: + return "+inf" exponents = { "0": "⁰", "1": "¹", From f43a9155d8712f9897e6c3061bca54c5c03e13ec Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Sun, 9 Oct 2022 18:46:23 +0530 Subject: [PATCH 08/18] Update number.py --- src/humanize/number.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 15adcb9..7beb8ea 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -2,6 +2,7 @@ """Humanizing functions for numbers.""" from __future__ import annotations +from cmath import isnan import math import re @@ -328,15 +329,11 @@ def fractional(value: NumberOrString) -> str: Returns: str: Fractional number as a string. """ - if math.isnan(value): - return "nan" - if math.isinf(value) and value < 0: - return "-inf" - elif math.isinf(value) and value > 0: - return "+inf" try: number = float(value) - except (TypeError, ValueError): + if math.isnan(number): + raise ValueError + except (TypeError, ValueError, OverflowError): return str(value) whole_number = int(number) frac = Fraction(number - whole_number).limit_denominator(1000) @@ -384,12 +381,6 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: Returns: str: Number in scientific notation z.wq x 10ⁿ. """ - if math.isnan(value): - return "nan" - if math.isinf(value) and value < 0: - return "-inf" - elif math.isinf(value) and value > 0: - return "+inf" exponents = { "0": "⁰", "1": "¹", @@ -405,7 +396,9 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: } try: value = float(value) - except (ValueError, TypeError): + if math.isnan(value): + raise ValueError + except (ValueError, TypeError, OverflowError): return str(value) fmt = "{:.%se}" % str(int(precision)) n = fmt.format(value) From 14cbcb145f370e5b41171a285ed3f51d62da1069 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 9 Oct 2022 13:17:07 +0000 Subject: [PATCH 09/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/humanize/number.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 7beb8ea..de23d25 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -2,7 +2,6 @@ """Humanizing functions for numbers.""" from __future__ import annotations -from cmath import isnan import math import re From c0a110628bcc3f8084075b6e2a674b1056e9e3da Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Wed, 12 Oct 2022 21:57:39 +0530 Subject: [PATCH 10/18] Update number.py --- src/humanize/number.py | 45 ++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index de23d25..60c96ed 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -26,6 +26,17 @@ NumberOrString: TypeAlias = "float | str" +def format_non_finite(value: float) -> str: + if math.isnan(value): + return "NaN" + if math.isinf(value) and value < 0: + return "-Inf" + elif math.isinf(value) and value > 0: + return "+Inf" + else: + return "" + + def ordinal(value: NumberOrString, gender: str = "male") -> str: """Converts an integer to its ordinal as a string. @@ -63,8 +74,10 @@ def ordinal(value: NumberOrString, gender: str = "male") -> str: str: Ordinal string. """ try: + if (value is math.nan) or (value == math.inf) or (value == -math.inf): + return format_non_finite(float(value)) value = int(value) - except (TypeError, ValueError, OverflowError): + except (TypeError, ValueError): return str(value) if gender == "male": t = ( @@ -141,6 +154,8 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: value = int(value) else: float(value) + if not math.isfinite(value): + return format_non_finite(value) except (TypeError, ValueError): return str(value) @@ -208,8 +223,10 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str: be coaxed into an `int`. """ try: + if (value is math.nan) or (value == math.inf) or (value == -math.inf): + return format_non_finite(float(value)) value = int(value) - except (TypeError, ValueError, OverflowError): + except (TypeError, ValueError): return str(value) if value < 0: @@ -271,8 +288,10 @@ def apnumber(value: NumberOrString) -> str: is returned. """ try: + if (value is math.nan) or (value == math.inf) or (value == -math.inf): + return format_non_finite(float(value)) value = int(value) - except (TypeError, ValueError, OverflowError): + except (TypeError, ValueError): return str(value) if not 0 <= value < 10: return str(value) @@ -330,9 +349,9 @@ def fractional(value: NumberOrString) -> str: """ try: number = float(value) - if math.isnan(number): - raise ValueError - except (TypeError, ValueError, OverflowError): + if not math.isfinite(number): + return format_non_finite(number) + except (TypeError, ValueError): return str(value) whole_number = int(number) frac = Fraction(number - whole_number).limit_denominator(1000) @@ -395,9 +414,9 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: } try: value = float(value) - if math.isnan(value): - raise ValueError - except (ValueError, TypeError, OverflowError): + if not math.isfinite(value): + return format_non_finite(value) + except (ValueError, TypeError): return str(value) fmt = "{:.%se}" % str(int(precision)) n = fmt.format(value) @@ -520,12 +539,8 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str: Returns: str: """ - if math.isnan(value): - return "NaN" - if math.isinf(value) and value < 0: - return "-Inf" - elif math.isinf(value) and value > 0: - return "+Inf" + if not math.isfinite(value): + return format_non_finite(value) exponent = int(math.floor(math.log10(abs(value)))) if value != 0 else 0 if exponent >= 27 or exponent < -24: From 05f6c0fd8b4638cdd368bd530759e2a156363690 Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Wed, 12 Oct 2022 22:00:00 +0530 Subject: [PATCH 11/18] Add docstring in number.py --- src/humanize/number.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/humanize/number.py b/src/humanize/number.py index 60c96ed..f294d21 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -27,6 +27,8 @@ def format_non_finite(value: float) -> str: + """Utility function to handle infinite and nan cases. + """ if math.isnan(value): return "NaN" if math.isinf(value) and value < 0: From 9245532ed775298bbf47756f3477f1425bbcbd85 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Oct 2022 16:32:18 +0000 Subject: [PATCH 12/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/humanize/number.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index f294d21..c9cd007 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -27,8 +27,7 @@ def format_non_finite(value: float) -> str: - """Utility function to handle infinite and nan cases. - """ + """Utility function to handle infinite and nan cases.""" if math.isnan(value): return "NaN" if math.isinf(value) and value < 0: From b46e0d5d86810316e19a950613f7b3fc8ab49315 Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Wed, 12 Oct 2022 23:14:25 +0530 Subject: [PATCH 13/18] Add tests --- src/humanize/number.py | 13 ++++++++----- tests/test_number.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index c9cd007..f8813a6 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -75,7 +75,7 @@ def ordinal(value: NumberOrString, gender: str = "male") -> str: str: Ordinal string. """ try: - if (value is math.nan) or (value == math.inf) or (value == -math.inf): + if not math.isfinite(float(value)): return format_non_finite(float(value)) value = int(value) except (TypeError, ValueError): @@ -147,6 +147,8 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: thousands_sep = thousands_separator() decimal_sep = decimal_separator() try: + if not math.isfinite(float(value)): + return format_non_finite(float(value)) if isinstance(value, str): value = value.replace(thousands_sep, "").replace(decimal_sep, ".") if "." in value: @@ -155,8 +157,6 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: value = int(value) else: float(value) - if not math.isfinite(value): - return format_non_finite(value) except (TypeError, ValueError): return str(value) @@ -224,7 +224,7 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str: be coaxed into an `int`. """ try: - if (value is math.nan) or (value == math.inf) or (value == -math.inf): + if not math.isfinite(float(value)): return format_non_finite(float(value)) value = int(value) except (TypeError, ValueError): @@ -289,7 +289,7 @@ def apnumber(value: NumberOrString) -> str: is returned. """ try: - if (value is math.nan) or (value == math.inf) or (value == -math.inf): + if not math.isfinite(float(value)): return format_non_finite(float(value)) value = int(value) except (TypeError, ValueError): @@ -484,6 +484,9 @@ def clamp( if value is None: return None + if not math.isfinite(value): + return format_non_finite(value) + if floor is not None and value < floor: value = floor token = floor_token diff --git a/tests/test_number.py b/tests/test_number.py index 7a77daa..9f57c64 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -26,6 +26,11 @@ ("111", "111th"), ("something else", "something else"), (None, "None"), + (math.nan, "NaN"), + (math.inf, "+Inf"), + (-math.inf, "-Inf"), + ("nan", "NaN"), + ("-inf", "-Inf"), ], ) def test_ordinal(test_input: str, expected: str) -> None: @@ -64,6 +69,11 @@ def test_ordinal(test_input: str, expected: str) -> None: ([1234.5454545, 2], "1,234.55"), ([1234.5454545, 3], "1,234.545"), ([1234.5454545, 10], "1,234.5454545000"), + ([math.nan], "NaN"), + ([math.inf], "+Inf"), + ([-math.inf], "-Inf"), + (["nan"], "NaN"), + (["-inf"], "-Inf"), ], ) def test_intcomma( @@ -108,6 +118,11 @@ def test_intword_powers() -> None: ([None], "None"), (["1230000", "%0.2f"], "1.23 million"), ([10**101], "1" + "0" * 101), + ([math.nan], "NaN"), + ([math.inf], "+Inf"), + ([-math.inf], "-Inf"), + (["nan"], "NaN"), + (["-inf"], "-Inf"), ], ) def test_intword(test_args: list[str], expected: str) -> None: @@ -126,6 +141,11 @@ def test_intword(test_args: list[str], expected: str) -> None: (10, "10"), ("7", "seven"), (None, "None"), + (math.nan, "NaN"), + (math.inf, "+Inf"), + (-math.inf, "-Inf"), + ("nan", "NaN"), + ("-inf", "-Inf"), ], ) def test_apnumber(test_input: int | str, expected: str) -> None: @@ -147,6 +167,11 @@ def test_apnumber(test_input: int | str, expected: str) -> None: (1.5, "1 1/2"), (0.3, "3/10"), (0.333, "333/1000"), + (math.nan, "NaN"), + (math.inf, "+Inf"), + (-math.inf, "-Inf"), + ("nan", "NaN"), + ("-inf", "-Inf"), ], ) def test_fractional(test_input: float | str, expected: str) -> None: @@ -173,6 +198,11 @@ def test_fractional(test_input: float | str, expected: str) -> None: ([float(2e-20)], "2.00 x 10⁻²⁰"), ([float(-3e20)], "-3.00 x 10²⁰"), ([float(-4e-20)], "-4.00 x 10⁻²⁰"), + ([math.nan], "NaN"), + ([math.inf], "+Inf"), + ([-math.inf], "-Inf"), + (["nan"], "NaN"), + (["-inf"], "-Inf"), ], ) def test_scientific(test_args: list[typing.Any], expected: str) -> None: @@ -190,6 +220,9 @@ def test_scientific(test_args: list[typing.Any], expected: str) -> None: ([0.0001, "{:.0%}", 0.01, None, "under ", None], "under 1%"), ([0.9999, "{:.0%}", None, 0.99, None, "above "], "above 99%"), ([1, humanize.intword, 1e6, None, "under "], "under 1.0 million"), + ([math.nan], "NaN"), + ([math.inf], "+Inf"), + ([-math.inf], "-Inf"), ], ) def test_clamp(test_args: list[typing.Any], expected: str) -> None: From e5437987269bfc7fc7f28519c9860c0662621e63 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Oct 2022 17:44:50 +0000 Subject: [PATCH 14/18] [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, 1 insertion(+), 1 deletion(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index f8813a6..1913b61 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -148,7 +148,7 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: decimal_sep = decimal_separator() try: if not math.isfinite(float(value)): - return format_non_finite(float(value)) + return format_non_finite(float(value)) if isinstance(value, str): value = value.replace(thousands_sep, "").replace(decimal_sep, ".") if "." in value: From 6b33558bb027a3f7f3ffd46f9cb3d10c9bceb432 Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Wed, 12 Oct 2022 23:30:02 +0530 Subject: [PATCH 15/18] Update number.py --- src/humanize/number.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 1913b61..8cd8e48 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -147,15 +147,17 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: thousands_sep = thousands_separator() decimal_sep = decimal_separator() try: - if not math.isfinite(float(value)): - return format_non_finite(float(value)) if isinstance(value, str): value = value.replace(thousands_sep, "").replace(decimal_sep, ".") + if not math.isfinite(float(value)): + return format_non_finite(float(value)) if "." in value: value = float(value) else: value = int(value) else: + if not math.isfinite(float(value)): + return format_non_finite(float(value)) float(value) except (TypeError, ValueError): return str(value) From e7f5c2b2fc0c38efb2981a406c22be2e01188c80 Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Thu, 13 Oct 2022 00:59:55 +0530 Subject: [PATCH 16/18] Update number.py --- src/humanize/number.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 8cd8e48..adf3ca0 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -34,8 +34,7 @@ def format_non_finite(value: float) -> str: return "-Inf" elif math.isinf(value) and value > 0: return "+Inf" - else: - return "" + return "" def ordinal(value: NumberOrString, gender: str = "male") -> str: From b72d21ccbc6fbd3eff63ad7cc9b1dfb0c0490b61 Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Thu, 13 Oct 2022 10:28:02 +0530 Subject: [PATCH 17/18] Update number.py --- src/humanize/number.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index adf3ca0..d44a1a8 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -26,7 +26,7 @@ NumberOrString: TypeAlias = "float | str" -def format_non_finite(value: float) -> str: +def _format_not_finite(value: float) -> str: """Utility function to handle infinite and nan cases.""" if math.isnan(value): return "NaN" @@ -75,7 +75,7 @@ def ordinal(value: NumberOrString, gender: str = "male") -> str: """ try: if not math.isfinite(float(value)): - return format_non_finite(float(value)) + return _format_not_finite(float(value)) value = int(value) except (TypeError, ValueError): return str(value) @@ -149,14 +149,14 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: if isinstance(value, str): value = value.replace(thousands_sep, "").replace(decimal_sep, ".") if not math.isfinite(float(value)): - return format_non_finite(float(value)) + return _format_not_finite(float(value)) if "." in value: value = float(value) else: value = int(value) else: if not math.isfinite(float(value)): - return format_non_finite(float(value)) + return _format_not_finite(float(value)) float(value) except (TypeError, ValueError): return str(value) @@ -226,7 +226,7 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str: """ try: if not math.isfinite(float(value)): - return format_non_finite(float(value)) + return _format_not_finite(float(value)) value = int(value) except (TypeError, ValueError): return str(value) @@ -291,7 +291,7 @@ def apnumber(value: NumberOrString) -> str: """ try: if not math.isfinite(float(value)): - return format_non_finite(float(value)) + return _format_not_finite(float(value)) value = int(value) except (TypeError, ValueError): return str(value) @@ -352,7 +352,7 @@ def fractional(value: NumberOrString) -> str: try: number = float(value) if not math.isfinite(number): - return format_non_finite(number) + return _format_not_finite(number) except (TypeError, ValueError): return str(value) whole_number = int(number) @@ -417,7 +417,7 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: try: value = float(value) if not math.isfinite(value): - return format_non_finite(value) + return _format_not_finite(value) except (ValueError, TypeError): return str(value) fmt = "{:.%se}" % str(int(precision)) @@ -486,7 +486,7 @@ def clamp( return None if not math.isfinite(value): - return format_non_finite(value) + return _format_not_finite(value) if floor is not None and value < floor: value = floor @@ -545,7 +545,7 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str: str: """ if not math.isfinite(value): - return format_non_finite(value) + return _format_not_finite(value) exponent = int(math.floor(math.log10(abs(value)))) if value != 0 else 0 if exponent >= 27 or exponent < -24: From e64ad9e8728c5f8f434a6fda98336adc7fb0bf2d Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Mon, 17 Oct 2022 12:12:59 +0300 Subject: [PATCH 18/18] Simplify See https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/no-else-return.html --- src/humanize/number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index d44a1a8..f060b79 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -32,7 +32,7 @@ def _format_not_finite(value: float) -> str: return "NaN" if math.isinf(value) and value < 0: return "-Inf" - elif math.isinf(value) and value > 0: + if math.isinf(value) and value > 0: return "+Inf" return ""