Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add interpolate argument to avoid resolving proxied values.
Browse files Browse the repository at this point in the history
The argument is already documented but not implemented yet.
Fixes joke2k#415
David-Wobrock committed Jul 14, 2023

Unverified

No user is associated with the committer email.
1 parent 2750dd5 commit 120d030
Showing 4 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@ Added
`#468 <https://github.com/joke2k/django-environ/pull/468>`_.
- Added capability to handle comments after #, after quoted values, like ``KEY= 'part1 # part2' # comment``
`#475 <https://github.com/joke2k/django-environ/pull/475>`_.
- Added support for ``interpolate`` parameter
`#419 <https://github.com/joke2k/django-environ/pull/419>`_.

Changed
+++++++
2 changes: 1 addition & 1 deletion docs/tips.rst
Original file line number Diff line number Diff line change
@@ -226,7 +226,7 @@ Proxy value
===========

Values that being with a ``$`` may be interpolated. Pass ``interpolate=True`` to
``environ.Env()`` to enable this feature:
``environ.Env()`` to enable this feature (``True`` by default):

.. code-block:: python
6 changes: 4 additions & 2 deletions environ/environ.py
Original file line number Diff line number Diff line change
@@ -197,11 +197,12 @@ class Env:
VAR = re.compile(r'(?<!\\)\$\{?(?P<name>[A-Z_][0-9A-Z_]*)}?',
re.IGNORECASE)

def __init__(self, **scheme):
def __init__(self, interpolate=True, **scheme):
self._local = threading.local()
self.smart_cast = True
self.escape_proxy = False
self.prefix = ""
self.interpolate = interpolate
self.scheme = scheme

def __call__(self, var, cast=None, default=NOTSET, parse_default=False):
@@ -425,7 +426,8 @@ def _get_value(self, var_name, cast=None, default=NOTSET,
value = default

# Expand variables
if isinstance(value, (bytes, str)) and var_name not in NOT_EXPANDED:
if self.interpolate and isinstance(value, (bytes, str)) \
and var_name not in NOT_EXPANDED:
def repl(match_):
return self.get_value(
match_.group('name'), cast=cast, default=default,
4 changes: 4 additions & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
@@ -134,6 +134,10 @@ def test_bool_true(self, value, variable):
def test_proxied_value(self):
assert self.env('PROXIED_VAR') == 'bar'

def test_not_interpolated_proxied_value(self):
env = Env(interpolate=False)
assert env('PROXIED_VAR') == '$STR_VAR'

def test_escaped_dollar_sign(self):
self.env.escape_proxy = True
assert self.env('ESCAPED_VAR') == '$baz'

0 comments on commit 120d030

Please sign in to comment.