Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for empty var with None default value #332

Merged
merged 2 commits into from
Oct 6, 2021
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: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Added
- Added option to override existing variables with ``read_env``
`#103 <https://github.com/joke2k/django-environ/issues/103>`_,
`#249 <https://github.com/joke2k/django-environ/issues/249>`_.
- Added support for empty var with None default value
`#209 <https://github.com/joke2k/django-environ/issues/209>`_.


Fixed
Expand Down
2 changes: 2 additions & 0 deletions environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ def get_value(self, var, cast=None, default=NOTSET, parse_default=False):
not isinstance(default, NoValue):
cast = type(default)

value = None if default is None and value == '' else value

if value != default or (parse_default and value):
value = self.parse_value(value, cast)

Expand Down
1 change: 1 addition & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def test_int(self):

def test_int_with_none_default(self):
assert self.env('NOT_PRESENT_VAR', cast=int, default=None) is None
assert self.env('EMPTY_INT_VAR', cast=int, default=None) is None

@pytest.mark.parametrize(
'value,variable',
Expand Down
1 change: 1 addition & 0 deletions tests/test_env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ FLOAT_STRANGE_VAR2=123.420.333,3
FLOAT_NEGATIVE_VAR=-1.0
PROXIED_VAR=$STR_VAR
EMPTY_LIST=
EMPTY_INT_VAR=
INT_VAR=42
STR_LIST_WITH_SPACES= foo, bar
STR_VAR=bar
Expand Down