Skip to content

Commit

Permalink
Strip tokens in ListOf (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
willkg committed Jan 30, 2024
1 parent 153c6e5 commit 9147e3a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/everett/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,13 +540,19 @@ def get_key_from_envs(envs: Iterable[Any], key: str) -> Union[str, NoValue]:
class ListOf:
"""Parse a comma-separated list of things.
After delimiting items, this strips the whitespace at the beginning and end
of each string. Then it passes each string into the parser to get the final
value.
>>> from everett.manager import ListOf
>>> ListOf(str)('')
[]
>>> ListOf(str)('a,b,c,d')
['a', 'b', 'c', 'd']
>>> ListOf(int)('1,2,3,4')
[1, 2, 3, 4]
>>> ListOf(str)('1, 2 ,3,4')
['1', '2', '3', '4']
Note: This doesn't handle quotes or backslashes or any complicated string
parsing.
Expand All @@ -565,7 +571,7 @@ def __init__(self, parser: Callable, delimiter: str = ","):
def __call__(self, value: str) -> List[Any]:
parser = get_parser(self.sub_parser)
if value:
return [parser(token) for token in value.split(self.delimiter)]
return [parser(token.strip()) for token in value.split(self.delimiter)]
else:
return []

Expand Down
1 change: 1 addition & 0 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ def test_ListOf():
assert ListOf(str)("foo") == ["foo"]
assert ListOf(bool)("t,f") == [True, False]
assert ListOf(int)("1,2,3") == [1, 2, 3]
assert ListOf(str)("1 , 2, 3") == ["1", "2", "3"]
assert ListOf(int, delimiter=":")("1:2") == [1, 2]


Expand Down

0 comments on commit 9147e3a

Please sign in to comment.