Skip to content

Commit 9147e3a

Browse files
committed
Strip tokens in ListOf (willkg#241)
1 parent 153c6e5 commit 9147e3a

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/everett/manager.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,19 @@ def get_key_from_envs(envs: Iterable[Any], key: str) -> Union[str, NoValue]:
540540
class ListOf:
541541
"""Parse a comma-separated list of things.
542542
543+
After delimiting items, this strips the whitespace at the beginning and end
544+
of each string. Then it passes each string into the parser to get the final
545+
value.
546+
543547
>>> from everett.manager import ListOf
544548
>>> ListOf(str)('')
545549
[]
546550
>>> ListOf(str)('a,b,c,d')
547551
['a', 'b', 'c', 'd']
548552
>>> ListOf(int)('1,2,3,4')
549553
[1, 2, 3, 4]
554+
>>> ListOf(str)('1, 2 ,3,4')
555+
['1', '2', '3', '4']
550556
551557
Note: This doesn't handle quotes or backslashes or any complicated string
552558
parsing.
@@ -565,7 +571,7 @@ def __init__(self, parser: Callable, delimiter: str = ","):
565571
def __call__(self, value: str) -> List[Any]:
566572
parser = get_parser(self.sub_parser)
567573
if value:
568-
return [parser(token) for token in value.split(self.delimiter)]
574+
return [parser(token.strip()) for token in value.split(self.delimiter)]
569575
else:
570576
return []
571577

tests/test_manager.py

+1
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ def test_ListOf():
274274
assert ListOf(str)("foo") == ["foo"]
275275
assert ListOf(bool)("t,f") == [True, False]
276276
assert ListOf(int)("1,2,3") == [1, 2, 3]
277+
assert ListOf(str)("1 , 2, 3") == ["1", "2", "3"]
277278
assert ListOf(int, delimiter=":")("1:2") == [1, 2]
278279

279280

0 commit comments

Comments
 (0)