From 8c1b1c1ce09e617e7bc3561f89f5c6beffd6ba0c Mon Sep 17 00:00:00 2001 From: David Wobrock Date: Mon, 26 Sep 2022 15:19:47 +0200 Subject: [PATCH] Add interpolate argument to avoid resolving proxied values. The argument is already documented but not implemented yet. Fixes #415 --- CHANGELOG.rst | 2 ++ docs/tips.rst | 2 +- environ/environ.py | 6 ++++-- tests/test_env.py | 4 ++++ 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index aa7b3ff8..8e181b19 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -17,6 +17,8 @@ Added `#468 `_. - Added capability to handle comments after #, after quoted values, like ``KEY= 'part1 # part2' # comment`` `#475 `_. +- Added support for ``interpolate`` parameter + `#419 `_. Changed +++++++ diff --git a/docs/tips.rst b/docs/tips.rst index 20b8c3e1..ab59f691 100644 --- a/docs/tips.rst +++ b/docs/tips.rst @@ -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 diff --git a/environ/environ.py b/environ/environ.py index 644286e1..f35470c5 100644 --- a/environ/environ.py +++ b/environ/environ.py @@ -197,11 +197,12 @@ class Env: VAR = re.compile(r'(?[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, diff --git a/tests/test_env.py b/tests/test_env.py index 0b105f55..1f2df156 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -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'