示例#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
示例#2
0
 def test_dict(self):
     self.assertIs(
         extract_nested_type(SampleOuterConfig,
                             ["my_list_of_schemas", "42", "my_default"]),
         int)
示例#3
0
 def test_list(self):
     self.assertIs(
         extract_nested_type(SampleOuterConfig,
                             ["my_dict_of_schemas", "key", "my_help_text"]),
         str)
示例#4
0
 def test_optional(self):
     self.assertIs(
         extract_nested_type(SampleOuterConfig,
                             ["my_schema", "my_optional_str"]),
         str)
示例#5
0
 def test_empty(self):
     self.assertIs(extract_nested_type(SampleConfigTypes, []), SampleConfigTypes)