Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ The following are all type-casting methods of `Env`:
- `env.int`
- `env.float`
- `env.decimal`
- `env.list` (accepts optional `subcast` keyword argument)
- `env.list` (accepts optional `subcast` and `delimiter` keyword arguments)
- `env.dict` (accepts optional `subcast_keys` and `subcast_values` keyword arguments)
- `env.json`
- `env.datetime`
Expand Down
6 changes: 4 additions & 2 deletions environs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@ def _make_list_field(*, subcast: typing.Optional[type], **kwargs) -> ma.fields.L
return ma.fields.List(inner_field, **kwargs)


def _preprocess_list(value: typing.Union[str, typing.Iterable], **kwargs) -> typing.Iterable:
def _preprocess_list(
value: typing.Union[str, typing.Iterable], *, delimiter: str = ",", **kwargs
) -> typing.Iterable:
if ma.utils.is_iterable_but_not_string(value):
return value
return typing.cast(str, value).split(",") if value != "" else []
return typing.cast(str, value).split(delimiter) if value != "" else []


def _preprocess_dict(
Expand Down
4 changes: 4 additions & 0 deletions tests/test_environs.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def test_list_with_spaces(self, set_env, env):
set_env({"LIST": " 1, 2,3"})
assert env.list("LIST", subcast=int) == [1, 2, 3]

def test_list_with_spaces_as_delimiter(self, set_env, env):
set_env({"LIST": "a b c"})
assert env.list("LIST", delimiter=" ") == ["a", "b", "c"]

def test_dict(self, set_env, env):
set_env({"DICT": "key1=1,key2=2"})
assert env.dict("DICT") == {"key1": "1", "key2": "2"}
Expand Down