Skip to content

Commit

Permalink
list_string now works with any Iterable that yields strings
Browse files Browse the repository at this point in the history
  • Loading branch information
gmatteo committed May 24, 2024
1 parent e0b72dd commit b8f3726
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions monty/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, cast
from typing import TYPE_CHECKING, cast, Iterable

if TYPE_CHECKING:
from typing import Any, Union
Expand Down Expand Up @@ -34,7 +34,7 @@ def is_string(s: Any) -> bool:
return False


def list_strings(arg: Union[str, list[str]]) -> list[str]:
def list_strings(arg: Union[str, Iterable[str]]) -> list[str]:
"""
Always return a list of strings, given a string or list of strings as
input.
Expand All @@ -48,15 +48,17 @@ def list_strings(arg: Union[str, list[str]]) -> list[str]:
>>> list_strings(['A','list','of','strings'])
['A', 'list', 'of', 'strings']
>>> list_strings(('A','list','of','strings'))
['A', 'list', 'of', 'strings']
>>> list_strings({"a": 1, "b": 2}.keys())
['a', 'b']
"""
if is_string(arg):
return [cast(str, arg)]

elif isinstance(arg, list):
return arg

else:
raise TypeError("Wrong type, expect str or list[str].")
return [cast(str, s) for s in arg]


def marquee(text: str = "", width: int = 78, mark: str = "*") -> str:
Expand Down

0 comments on commit b8f3726

Please sign in to comment.