Loading a config from a dictionary or file does not use the specified default value
Closed this issue · 1 comments
stephen-bunn commented
Expected Behavior
I would expect when loading a config from a dictionary, yaml, json, etc. that if a given value doesn't exist the default specified in the Config class would be used.
from file_config import config, var
@config
class TestConfig(object):
name = var(str, default="MyName", required=False)
cfg = TestConfig.loads_json("""{}""")
assert cfg.name == "MyName" # equals True
Current Behavior
Currently loading a config from a dictionary, yaml, json, etc. does not use the default if it doesn't exist. Instead it just sets the value to None...
from file_config import config, var
@config
class TestConfig(object):
name = var(str, default="MyName", required=False)
cfg = TestConfig.loads_json("""{}""")
assert cfg.name == "MyConfig" # raises AssertionError
# currently cfg.name == None
Possible Solution
Luckly the solution looks pretty straight forward.
At https://github.com/stephen-bunn/file-config/blob/master/src/file_config/_file_config.py#L328
If we just replace None
with arg_default
the specified default value will be used. Note that arg_default
is set to None
if no value is given as the default (so prior implementations shouldn't break).
Your Environment
- File Config Version: 0.3.4
- Python Version: 3.7+
- Operating System and Version: Ubuntu 18.10
❤️ Thank you!
stephen-bunn commented
Closed with #22