Skip to content

Commit

Permalink
Merge pull request #332 from mehdy/default-none
Browse files Browse the repository at this point in the history
Add support for empty var with None default value
  • Loading branch information
sergeyklay committed Oct 6, 2021
2 parents 972b103 + 26fb010 commit 7dc1c1b
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 0 deletions.
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

0 comments on commit 7dc1c1b

Please sign in to comment.