Skip to content

Commit 67e7297

Browse files
committed
update as_list() utility to explicitly support more input types
1 parent 7457331 commit 67e7297

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

web_poet/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import weakref
2+
from collections.abc import Iterable
23
from functools import wraps
34
from typing import Any, Optional, List
45

@@ -29,9 +30,20 @@ def as_list(value: Optional[Any]) -> List[Any]:
2930
[123]
3031
>>> as_list(["foo", "bar", 123])
3132
['foo', 'bar', 123]
33+
>>> as_list(("foo", "bar", 123))
34+
['foo', 'bar', 123]
35+
>>> as_list(range(5))
36+
[0, 1, 2, 3, 4]
37+
>>> def gen():
38+
... yield 1
39+
... yield 2
40+
>>> as_list(gen())
41+
[1, 2]
3242
"""
3343
if value is None:
3444
return []
35-
if not isinstance(value, list):
45+
if isinstance(value, str):
46+
return [value]
47+
if not isinstance(value, Iterable):
3648
return [value]
3749
return list(value)

0 commit comments

Comments
 (0)