Skip to content

Commit

Permalink
Merge pull request joke2k#323 from joke2k/fix/bytes-default
Browse files Browse the repository at this point in the history
Use default option in Env.bytes()
  • Loading branch information
sergeyklay committed Sep 5, 2021
2 parents 30e06c8 + 09dbc01 commit d43ffd5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ Added
(`#297 <https://github.com/joke2k/django-environ/pull/297>`_)
- Added Elasticsearch7 to search scheme
(`#314 <https://github.com/joke2k/django-environ/pull/314>`_)
- Provided ability to use ``bytes`` or ``str`` as a default value for ``Env.bytes()``


Fixed
+++++
- Fixed links in the documentation
- Use default option in ``Env.bytes()``
(`#206 <https://github.com/joke2k/django-environ/pull/206>`_)

Changed
+++++++
Expand All @@ -34,13 +37,15 @@ Added
- Django 3.1 and 3.2 are now supported
- Added missed classifiers to ``setup.py``
- Accept Python 3.6 path-like objects for ``read_env``
(`#106 <https://github.com/joke2k/django-environ/issues/106>`_, `#286 <https://github.com/joke2k/django-environ/issues/286>`_)

Fixed
+++++
- Fixed various code linting errors
- Fixed typos in the documentation
- Added missed files to the package contents
- Fixed ``db_url_config`` to work the same for all postgres-like schemes
(`#264 <https://github.com/joke2k/django-environ/issues/264>`_, `#268 <https://github.com/joke2k/django-environ/issues/268>`_)

Changed
+++++++
Expand Down
10 changes: 7 additions & 3 deletions environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ def bytes(self, var, default=NOTSET, encoding='utf8'):
"""
:rtype: bytes
"""
return self.get_value(var, cast=str).encode(encoding)
value = self.get_value(var, cast=str, default=default)
if hasattr(value, 'encode'):
return value.encode(encoding)
return value

def bool(self, var, default=NOTSET):
"""
Expand Down Expand Up @@ -360,8 +363,9 @@ def get_value(self, var, cast=None, default=NOTSET, parse_default=False):
value = default

# Resolve any proxied values
if hasattr(value, 'startswith') and value.startswith('$'):
value = value.lstrip('$')
prefix = b'$' if isinstance(value, bytes) else '$'
if hasattr(value, 'startswith') and value.startswith(prefix):
value = value.lstrip(prefix)
value = self.get_value(value, cast=cast, default=default)

# Smart casting
Expand Down
12 changes: 10 additions & 2 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,16 @@ def test_str(self, var, val, multiline):
assert self.env(var) == val
assert self.env.str(var, multiline=multiline) == val

def test_bytes(self):
assert_type_and_value(bytes, b'bar', self.env.bytes('STR_VAR'))
@pytest.mark.parametrize(
'var,val,default',
[
('STR_VAR', b'bar', Env.NOTSET),
('NON_EXISTENT_BYTES_VAR', b'some-default', b'some-default'),
('NON_EXISTENT_STR_VAR', b'some-default', 'some-default'),
]
)
def test_bytes(self, var, val, default):
assert_type_and_value(bytes, val, self.env.bytes(var, default=default))

def test_int(self):
assert_type_and_value(int, 42, self.env('INT_VAR', cast=int))
Expand Down

0 comments on commit d43ffd5

Please sign in to comment.