From 14d76fb143eb579590952f85d309aaab999f8317 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 22 Apr 2023 12:46:25 +1200 Subject: [PATCH] Fix pre-commit errors --- src/humanize/lists.py | 16 +++++++++------- tests/test_lists.py | 6 +++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/humanize/lists.py b/src/humanize/lists.py index eb54dff..c870814 100644 --- a/src/humanize/lists.py +++ b/src/humanize/lists.py @@ -1,10 +1,13 @@ -from typing import AnyStr, List +"""Lists related humanization.""" +from typing import Any, List __all__ = ["naturallist"] -def naturallist(items: List[AnyStr]) -> str: - """Convert a list of items into a human-readable string with commas and 'and' +def naturallist(items: List[Any]) -> str: + """Natural list. + + Convert a list of items into a human-readable string with commas and 'and' Args: items (list): A list of strings @@ -18,10 +21,9 @@ def naturallist(items: List[AnyStr]) -> str: >>> naturallist(["one"]) 'one' """ - if len(items) == 1: - return items[0] + return str(items[0]) elif len(items) == 2: - return f"{items[0]} and {items[1]}" + return f"{str(items[0])} and {str(items[1])}" else: - return ", ".join(items[:-1]) + f" and {items[-1]}" + return ", ".join(str(item) for item in items[:-1]) + f" and {str(items[-1])}" diff --git a/tests/test_lists.py b/tests/test_lists.py index 19651aa..fc7ed2b 100644 --- a/tests/test_lists.py +++ b/tests/test_lists.py @@ -11,7 +11,11 @@ ([["one", "two"]], "one and two"), ([["one"]], "one"), ([[""]], ""), + ([[1, 2, 3]], "1, 2 and 3"), + ([[1, "two"]], "1 and two"), ], ) -def test_naturallist(test_args, expected): +def test_naturallist( + test_args: list[str] | list[int] | list[str | int], expected: str +) -> None: assert humanize.naturallist(*test_args) == expected