Esempio n. 1
0
def override_config_dict(config_dict: Any, overrides: List[str]) -> Any:
    for override in overrides:
        try:
            key, _, value = override.rpartition("=")
            path = key.split(".")
            param_type = extract_nested_type(ConfigSchema, path)
            # this is a bit of a hack; we should do something better
            # but this is convenient for specifying lists of strings
            # e.g. edge_paths
            if has_origin(param_type, list):
                value = value.split(",")
            # Convert numbers (caution: ignore bools, which are ints)
            if isinstance(param_type, type) \
                    and issubclass(param_type, (int, float)) \
                    and not issubclass(param_type, bool):
                value = param_type(value)
            inject_nested_value(config_dict, path, value)
        except Exception as err:
            raise RuntimeError("Can't parse override: %s" % override) from err
    return config_dict
Esempio n. 2
0
 def test_mixed(self):
     data = {"foo": [{"bar": True, "baz": [42, 43]}]}
     inject_nested_value(data, ["foo", "0", "baz", "1"], 1)
     self.assertEqual(data, {"foo": [{"bar": True, "baz": [42, 1]}]})
Esempio n. 3
0
 def test_empty(self):
     with self.assertRaises(ValueError):
         inject_nested_value({"foo": 42}, [], {"bar": 43})