Skip to content

Commit

Permalink
Fix pre-commit errors
Browse files Browse the repository at this point in the history
  • Loading branch information
FPA-AkshayGollahalli committed Apr 23, 2023
1 parent ba6e627 commit 14d76fb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/humanize/lists.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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])}"
6 changes: 5 additions & 1 deletion tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 14d76fb

Please sign in to comment.