Skip to content

Commit

Permalink
Fixed bug in NANLAST/NANFIRST
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SethMMorton committed Feb 27, 2023
1 parent 4352d7b commit 0e7533d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
13 changes: 8 additions & 5 deletions natsort/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/test_natsorted.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions tests/test_parse_number_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 0e7533d

Please sign in to comment.