summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Morton <seth.m.morton@gmail.com>2023-02-27 08:38:56 -0800
committerSeth Morton <seth.m.morton@gmail.com>2023-02-27 08:41:39 -0800
commit0e7533d2799c4bc8cc6968cd6485b6996eee0a05 (patch)
tree277ca9d989b772665238fb14564bc76aae48c9ac
parent4352d7b7b029a2636f8b5a68ebdb9ad86fbd860a (diff)
downloadnatsort-0e7533d2799c4bc8cc6968cd6485b6996eee0a05.tar.gz
Fixed bug in NANLAST/NANFIRST
The previous code change to make NaN and None ordering consistent made it so that NANLAST did not put NaN last. Oops. It also had made it so that NaN wasn't first for NANFIRST. Oops.
-rw-r--r--natsort/utils.py13
-rw-r--r--tests/test_natsorted.py4
-rw-r--r--tests/test_parse_number_function.py10
3 files changed, 15 insertions, 12 deletions
diff --git a/natsort/utils.py b/natsort/utils.py
index 2a3745f..dd8c39f 100644
--- a/natsort/utils.py
+++ b/natsort/utils.py
@@ -417,18 +417,21 @@ def parse_number_or_none_factory(
nan_replace = float("+inf") if alg & ns.NANLAST else float("-inf")
def func(
- val: Any, _nan_replace: float = nan_replace, _sep: StrOrBytes = sep
+ val: Any,
+ _nan_replace: float = nan_replace,
+ _sep: StrOrBytes = sep,
+ reverse: bool = nan_replace == float("+inf"),
) -> BasicTuple:
"""Given a number, place it in a tuple with a leading null string."""
# Add a trailing string numbers equaling _nan_replace. This will make
# the ordering between None NaN, and the NaN replacement value...
# None comes first, then NaN, then the replacement value.
- if val is None:
- return _sep, _nan_replace, "1"
- elif val != val:
+ if val != val:
+ return _sep, _nan_replace, "3" if reverse else "1"
+ elif val is None:
return _sep, _nan_replace, "2"
elif val == _nan_replace:
- return _sep, _nan_replace, "3"
+ return _sep, _nan_replace, "1" if reverse else "3"
else:
return _sep, val
diff --git a/tests/test_natsorted.py b/tests/test_natsorted.py
index e4a4788..bd1cc39 100644
--- a/tests/test_natsorted.py
+++ b/tests/test_natsorted.py
@@ -113,8 +113,8 @@ def test_natsorted_handles_mixed_types(
@pytest.mark.parametrize(
"alg, expected",
[
- (ns.DEFAULT, [None, float("nan"), float("-inf"), 5, "25", 1e40, float("inf")]),
- (ns.NANLAST, [float("-inf"), 5, "25", 1e40, None, float("nan"), float("inf")]),
+ (ns.DEFAULT, [float("nan"), None, float("-inf"), 5, "25", 1e40, float("inf")]),
+ (ns.NANLAST, [float("-inf"), 5, "25", 1e40, float("inf"), None, float("nan")]),
],
)
def test_natsorted_consistent_ordering_with_nan_and_friends(
diff --git a/tests/test_parse_number_function.py b/tests/test_parse_number_function.py
index 5ac5700..24ee714 100644
--- a/tests/test_parse_number_function.py
+++ b/tests/test_parse_number_function.py
@@ -35,17 +35,17 @@ def test_parse_number_factory_makes_function_that_returns_tuple(
(
ns.DEFAULT,
float("nan"),
- ("", float("-inf"), "2"),
+ ("", float("-inf"), "1"),
), # NaN transformed to -infinity
(
ns.NANLAST,
float("nan"),
- ("", float("+inf"), "2"),
+ ("", float("+inf"), "3"),
), # NANLAST makes it +infinity
- (ns.DEFAULT, None, ("", float("-inf"), "1")), # None transformed to -infinity
- (ns.NANLAST, None, ("", float("+inf"), "1")), # NANLAST makes it +infinity
+ (ns.DEFAULT, None, ("", float("-inf"), "2")), # None transformed to -infinity
+ (ns.NANLAST, None, ("", float("+inf"), "2")), # NANLAST makes it +infinity
(ns.DEFAULT, float("-inf"), ("", float("-inf"), "3")),
- (ns.NANLAST, float("+inf"), ("", float("+inf"), "3")),
+ (ns.NANLAST, float("+inf"), ("", float("+inf"), "1")),
],
)
def test_parse_number_factory_treats_nan_and_none_special(