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 22, 2023
1 parent ba6e627 commit 133a037
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
27 changes: 20 additions & 7 deletions src/humanize/lists.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
from typing import AnyStr, List
"""Lists related humanization."""
import sys
from typing import TYPE_CHECKING, List

__all__ = ["naturallist"]

if TYPE_CHECKING:
if sys.version_info >= (3, 10):
from typing import TypeAlias
else:
from typing_extensions import TypeAlias

# This type can be better defined by typing.SupportsInt, typing.SupportsFloat
# but that's a Python 3.8 only typing option.
NumberOrString: TypeAlias = "float | str"


def naturallist(items: List[AnyStr]) -> str:
"""Convert a list of items into a human-readable string with commas and 'and'
def naturallist(items: List[NumberOrString]) -> 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 +32,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 133a037

Please sign in to comment.