summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Morton <seth.m.morton@gmail.com>2023-02-26 21:05:46 -0800
committerSeth Morton <seth.m.morton@gmail.com>2023-02-26 21:06:22 -0800
commit90fd249a81a2e343d30de19b1671080597bce084 (patch)
treed55565cdfa0a622ba73bc629e4e4f4480ee974fa
parent3b6ea6a2bcc114c32320294106f47ad6ecfd6b14 (diff)
downloadnatsort-90fd249a81a2e343d30de19b1671080597bce084.tar.gz
Use isintance over duck typing
This makes what is happening a bit clearer, and plays with mypy much better.
-rw-r--r--natsort/utils.py39
1 files changed, 16 insertions, 23 deletions
diff --git a/natsort/utils.py b/natsort/utils.py
index 36cb436..733bb1b 100644
--- a/natsort/utils.py
+++ b/natsort/utils.py
@@ -329,7 +329,7 @@ def natsort_key(
--------
parse_string_factory
parse_bytes_factory
- parse_number_factory
+ parse_number_or_none_factory
"""
@@ -337,25 +337,18 @@ def natsort_key(
if key is not None:
val = key(val)
- # Assume the input are strings, which is the most common case
- try:
- return string_func(cast(str, val))
- except (TypeError, AttributeError):
- # If bytes type, use the bytes_func
- if type(val) in (bytes,):
- return bytes_func(cast(bytes, val))
-
- # Otherwise, assume it is an iterable that must be parsed recursively.
- # Do not apply the key recursively.
- try:
- return tuple(
- natsort_key(x, None, string_func, bytes_func, num_func)
- for x in cast(Iterable[Any], val)
- )
-
- # If that failed, it must be a number.
- except TypeError:
- return num_func(val)
+ if isinstance(val, (str, PurePath)):
+ return string_func(val)
+ elif isinstance(val, bytes):
+ return bytes_func(val)
+ elif isinstance(val, Iterable):
+ # Must be parsed recursively, but do not apply the key recursively.
+ return tuple(
+ natsort_key(x, None, string_func, bytes_func, num_func)
+ for x in val
+ )
+ else: # Anything else goes here
+ return num_func(val)
def parse_bytes_factory(alg: NSType) -> BytesTransformer:
@@ -863,9 +856,9 @@ def do_decoding(s: Any, encoding: str) -> Any:
*s* if *s* was not *bytes*.
"""
- try:
- return cast(bytes, s).decode(encoding)
- except (AttributeError, TypeError):
+ if isinstance(s, bytes):
+ return s.decode(encoding)
+ else:
return s