summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGilthans <gilthans@gmail.com>2022-08-11 12:02:15 +0300
committerGilthans <gilthans@gmail.com>2022-08-11 12:02:15 +0300
commit16f430a5daf0ab31acaad1dfdab09d2cdbae25ca (patch)
treed1e4612fb9d39106d4555ac9f6a87dc0e0830bf5
parente3c32f5638bf3a0e9a23633495269bea0e75d379 (diff)
downloadnatsort-16f430a5daf0ab31acaad1dfdab09d2cdbae25ca.tar.gz
Treat paths as strings in StrParser
-rw-r--r--natsort/utils.py14
-rw-r--r--tests/test_natsorted.py6
2 files changed, 15 insertions, 5 deletions
diff --git a/natsort/utils.py b/natsort/utils.py
index 3832318..2bceb85 100644
--- a/natsort/utils.py
+++ b/natsort/utils.py
@@ -103,14 +103,14 @@ TupleOfStrAnyPair = Tuple[Tuple[str], TupleOfAny]
FinalTransform = Union[TwoBlankTuple, TupleOfAny, TupleOfStrAnyPair]
FinalTransformer = Callable[[Iterable[Any], str], FinalTransform]
-# For the string parsing factory
-StrSplitter = Callable[[str], Iterable[str]]
-StrParser = Callable[[str], FinalTransform]
-
# For the path splitter
PathArg = Union[str, PurePath]
MatchFn = Callable[[str], Optional[Match]]
+# For the string parsing factory
+StrSplitter = Callable[[str], Iterable[str]]
+StrParser = Callable[[PathArg], FinalTransform]
+
# For the path parsing factory
PathSplitter = Callable[[PathArg], Tuple[FinalTransform, ...]]
@@ -493,7 +493,11 @@ def parse_string_factory(
normalize_input = _normalize_input_factory(alg)
compose_input = _compose_input_factory(alg) if alg & ns.LOCALEALPHA else _no_op
- def func(x: str) -> FinalTransform:
+ def func(x: PathArg) -> FinalTransform:
+ if isinstance(x, PurePath):
+ # While paths are technically not strings, it is natural for them
+ # to be treated the same.
+ x = str(x)
# Apply string input transformation function and return to x.
# Original function is usually a no-op, but some algorithms require it
# to also be the transformation function.
diff --git a/tests/test_natsorted.py b/tests/test_natsorted.py
index eb3aefe..1cbed83 100644
--- a/tests/test_natsorted.py
+++ b/tests/test_natsorted.py
@@ -5,6 +5,7 @@ See the README or the natsort homepage for more details.
"""
from operator import itemgetter
+from pathlib import PurePosixPath
from typing import List, Tuple, Union
import pytest
@@ -84,6 +85,11 @@ def test_natsorted_can_sort_as_version_numbers() -> None:
assert natsorted(given) == expected
+def test_natsorted_can_sorts_paths_same_as_strings() -> None:
+ paths = [PurePosixPath('a/1/something'), PurePosixPath('a/2/something'), PurePosixPath('a/10/something')]
+ assert [str(p) for p in natsorted(paths)] == natsorted([str(p) for p in paths])
+
+
@pytest.mark.parametrize(
"alg, expected",
[