From 27511032fda7a498eb7d631070754a3172daa499 Mon Sep 17 00:00:00 2001 From: Esben Sonne Date: Tue, 6 Feb 2018 13:19:55 +0100 Subject: [PATCH 1/2] Support static vars Hardcoded primitives fallthrough parsing and are returned as is. This comes in handy in the cases where something is required by other configs, but a static config is fine. It does make config kind of a three-way train crash, with parsing, defaults, and static vars. --- envargs/parser.py | 2 ++ tests/test_parsing.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/envargs/parser.py b/envargs/parser.py index 2c5e90c..ad2ac6a 100644 --- a/envargs/parser.py +++ b/envargs/parser.py @@ -27,6 +27,8 @@ def _load_values(values, fields): yield dest, value elif isinstance(field, dict): yield dest, dict(_load_values(values, field)) + elif isinstance(field, (int, float, str, bytes)): + yield dest, field def parse_dict(values, fields): diff --git a/tests/test_parsing.py b/tests/test_parsing.py index df74e38..90e9fde 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -232,3 +232,29 @@ def test_nesting(): 'an_int': 10, }, } + + +def test_static_vars(): + """Test that hardcoded vars fall through parsing.""" + args = { + 'a_var': Var( + use=int, + load_from='A_VAR', + ), + 'some_bytes': b'bytes!', + 'a_string': 'my string', + 'a_float': 1.5, + 'a_int': 1, + } + + values = { + 'A_VAR': '0', + } + + assert parse_dict(values, args) == { + 'a_var': 0, + 'some_bytes': b'bytes!', + 'a_string': 'my string', + 'a_float': 1.5, + 'a_int': 1, + } From 800bee3d5175797e919f6f15078f09ea25e92741 Mon Sep 17 00:00:00 2001 From: Esben Sonne Date: Tue, 6 Feb 2018 15:22:51 +0100 Subject: [PATCH 2/2] Support bools too --- envargs/parser.py | 2 +- tests/test_parsing.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/envargs/parser.py b/envargs/parser.py index ad2ac6a..3745050 100644 --- a/envargs/parser.py +++ b/envargs/parser.py @@ -27,7 +27,7 @@ def _load_values(values, fields): yield dest, value elif isinstance(field, dict): yield dest, dict(_load_values(values, field)) - elif isinstance(field, (int, float, str, bytes)): + elif isinstance(field, (int, float, str, bytes, bool)): yield dest, field diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 90e9fde..91555dc 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -245,6 +245,7 @@ def test_static_vars(): 'a_string': 'my string', 'a_float': 1.5, 'a_int': 1, + 'a_bool': True, } values = { @@ -257,4 +258,5 @@ def test_static_vars(): 'a_string': 'my string', 'a_float': 1.5, 'a_int': 1, + 'a_bool': True, }