From b8f3726282616ab74a9caeba146b6e489e03c938 Mon Sep 17 00:00:00 2001 From: Matteo Giantomassi Date: Fri, 24 May 2024 15:22:09 +0200 Subject: [PATCH 1/2] list_string now works with any Iterable that yields strings --- monty/string.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/monty/string.py b/monty/string.py index 70a05d0f..3e3c6878 100644 --- a/monty/string.py +++ b/monty/string.py @@ -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 @@ -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. @@ -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: From 50f17c35f6f19e0f3ab779e471a1e0950f588286 Mon Sep 17 00:00:00 2001 From: Matteo Giantomassi Date: Fri, 24 May 2024 15:31:03 +0200 Subject: [PATCH 2/2] Run ruff check --fix --- monty/string.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monty/string.py b/monty/string.py index 3e3c6878..1a56debc 100644 --- a/monty/string.py +++ b/monty/string.py @@ -4,7 +4,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, cast, Iterable +from typing import TYPE_CHECKING, Iterable, cast if TYPE_CHECKING: from typing import Any, Union