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