Skip to content

Commit

Permalink
update with new ruamel types
Browse files Browse the repository at this point in the history
  • Loading branch information
dhpitt committed Apr 30, 2024
1 parent 46394af commit afa3f6f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/configmypy/argparse_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def read_conf(self, config=None, **additional_config):
strict=True
elif self.infer_types == 'fuzzy':
strict=False

print(f"creating inferencer for {key} of type {type(value)}")
type_inferencer = TypeInferencer(orig_type=type(value), strict=strict)
else:
type_inferencer = type(value)
Expand Down
7 changes: 6 additions & 1 deletion src/configmypy/tests/test_argparse_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@


def test_ArgparseConfig(monkeypatch):
monkeypatch.setattr("sys.argv", ['test', '--data.batch_size', '24'])
monkeypatch.setattr("sys.argv", ['test', '--data.batch_size', '24',\
'--data.test_resolutions', '[16]'])
config = Bunch(TEST_CONFIG_DICT['default'])
parser = ArgparseConfig(infer_types='fuzzy')
args, kwargs = parser.read_conf(config)
assert args.data.test_resolutions == [16]
config.data.batch_size = 24
config.data.test_resolutions = [16]
assert config == args
# reset config data test_res
config.data.test_resolutions = [16,32]
assert kwargs == {}

# test ability to infer None, int-->float and iterables
Expand Down
18 changes: 15 additions & 3 deletions src/configmypy/tests/test_pipeline_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
data:
dataset: 'ns'
batch_size: 12
test_batch_sizes: [16,32]
other:
int_to_float: 1
float_to_none: 0.5
test:
opt:
Expand All @@ -20,23 +24,31 @@

TEST_CONFIG_DICT = {
'default': {'opt': {'optimizer': 'adam', 'lr': 0.1},
'data': {'dataset': 'ns', 'batch_size': 12}},
'data': {'dataset': 'ns', 'batch_size': 12, 'test_batch_sizes':[16,16]},
'other': {'int_to_float': 1, 'float_to_none': 0.5}},
'test': {'opt': {'optimizer': 'SGD'}}
}


def test_ConfigPipeline(mocker, monkeypatch):
""""Test for ConfigPipeline"""
mocker.patch("builtins.open", mocker.mock_open(read_data=TEST_CONFIG_FILE))
monkeypatch.setattr("sys.argv", ['test', '--data.batch_size', '24', '--config_file', 'config.yaml', '--config_name', 'test'])
monkeypatch.setattr("sys.argv", ['test', '--data.batch_size', '24', \
'--data.test_batch_sizes', '[16,16]',\
'--other.int_to_float', '0.05', \
'--other.float_to_none', 'None', \
'--config_file', 'config.yaml', \
'--config_name', 'test'])

true_config = Bunch(TEST_CONFIG_DICT['default'])
true_config.data.batch_size = 24
true_config.opt.optimizer = 'SGD'
true_config.other.int_to_float = 0.05
true_config.other.float_to_none = None

pipe = ConfigPipeline([
YamlConfig('./config.yaml', config_name='default'),
ArgparseConfig(config_file=None, config_name=None),
ArgparseConfig(config_file=None, config_name=None, infer_types='fuzzy'),
YamlConfig()
])
config = pipe.read_conf()
Expand Down
5 changes: 4 additions & 1 deletion src/configmypy/type_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from ast import literal_eval
from typing import Callable

# import custom Yaml types to handle sequences
from ruamel.yaml import CommentedSeq, CommentedMap


def infer_boolean(var, strict: bool=True):
"""
Expand Down Expand Up @@ -118,7 +121,7 @@ def __call__(self, var):
return infer_boolean(var, self.strict)
elif self.orig_type == float or self.orig_type == int:
return infer_numeric(var, self.strict)
elif self.orig_type == tuple or self.orig_type == list:
elif self.orig_type == tuple or self.orig_type == list or self.orig_type == CommentedMap or self.orig_type == CommentedSeq:
return infer_iterable(var, None, self.strict)
else:
if self.strict:
Expand Down

0 comments on commit afa3f6f

Please sign in to comment.