diff --git a/decouple.py b/decouple.py index 9873fc9..6f4d46d 100644 --- a/decouple.py +++ b/decouple.py @@ -275,7 +275,7 @@ def __init__(self, cast=text_type, delimiter=',', strip=string.whitespace, post_ def __call__(self, value): """The actual transformation""" if value is None: - return self.post_process() + value = '' transform = lambda s: self.cast(s.strip(self.strip)) diff --git a/tests/test_helper_csv.py b/tests/test_helper_csv.py index c5db287..9cab25e 100644 --- a/tests/test_helper_csv.py +++ b/tests/test_helper_csv.py @@ -1,5 +1,6 @@ # coding: utf-8 -from decouple import Csv +import pytest +from decouple import Config, Csv def test_csv(): @@ -34,3 +35,23 @@ def test_csv_quoted_parse(): def test_csv_none(): csv = Csv() assert [] == csv(None) + + +@pytest.mark.parametrize('value', [None, '']) +@pytest.mark.parametrize('post_process, expected', [ + (list, []), (tuple, ()), (sum, 0), (sorted, []), +]) +def test_csv_empty_post_process(value, post_process, expected): + csv = Csv(cast=int, post_process=post_process) + assert expected == csv(value) + + +def test_csv_none_default_with_custom_post_process(monkeypatch): + monkeypatch.delenv('CSV_OPTION', raising=False) + + def post_process(values): + return next(values, 'empty') + + csv = Csv(cast=pytest.fail, post_process=post_process) + config = Config({}) + assert 'empty' == config('CSV_OPTION', default=None, cast=csv)