def __init__(self, repo: IConfigRepository, overrides_list: List[Override]) -> None: self.override_choices = {} self.override_metadata = {} self.append_group_defaults = [] self.config_overrides = [] self.deletions = {} self.known_choices = {} self.known_choices_per_group = {} for override in overrides_list: if override.is_sweep_override(): continue is_group = repo.group_exists(override.key_or_group) value = override.value() is_dict = isinstance(override.value(), dict) if is_dict or not is_group: self.config_overrides.append(override) elif override.is_force_add(): # This could probably be made to work if there is a compelling use case. raise ConfigCompositionException( f"force-add of config groups is not supported: '{override.input_line}'" ) elif override.is_delete(): key = override.get_key_element()[1:] value = override.value() if value is not None and not isinstance(value, str): raise ValueError( f"Config group override deletion value must be a string : {override}" ) self.deletions[key] = Deletion(name=value) elif not isinstance(value, (str, list)): raise ValueError( f"Config group override must be a string or a list. Got {type(value).__name__}" ) elif override.is_add(): self.append_group_defaults.append( GroupDefault( group=override.key_or_group, package=override.package, value=value, external_append=True, )) else: key = override.get_key_element() self.override_choices[key] = value self.override_metadata[key] = OverrideMetadata( external_override=True)
def __init__(self, repo: IConfigRepository, overrides_list: List[Override]) -> None: self.override_choices = {} self.override_metadata = {} self.append_group_defaults = [] self.config_overrides = [] self.deletions = {} self.known_choices = {} self.known_choices_per_group = {} for override in overrides_list: if override.is_sweep_override(): continue is_group = repo.group_exists(override.key_or_group) value = override.value() is_dict = isinstance(override.value(), dict) if is_dict or not is_group: self.config_overrides.append(override) else: if override.is_delete(): key = override.get_key_element()[1:] value = override.value() if value is not None and not isinstance(value, str): raise ValueError( f"Config group override deletion value must be a string : {override}" ) self.deletions[key] = Deletion(name=value) elif not isinstance(value, (str, list)): raise ValueError( f"Config group override must be a string or a list. Got {type(value).__name__}" ) elif override.is_add(): self.append_group_defaults.append( GroupDefault( group=override.key_or_group, package=override.package, value=value, ) ) else: key = override.get_key_element() self.override_choices[key] = value self.override_metadata[key] = OverrideMetadata( external_override=True )
class TestConfigRepository: def test_config_repository_load(self, hydra_restore_singletons: Any, path: str) -> None: Plugins.instance() # initializes config_search_path = create_config_search_path(path) repo = ConfigRepository(config_search_path=config_search_path) ret = repo.load_config(config_path="dataset/imagenet.yaml") assert ret is not None assert ret.config == {"name": "imagenet", "path": "/datasets/imagenet"} assert repo.load_config(config_path="not_found.yaml") is None def test_config_repository_exists(self, hydra_restore_singletons: Any, path: str) -> None: Plugins.instance() # initializes config_search_path = create_config_search_path(path) repo = ConfigRepository(config_search_path=config_search_path) assert repo.config_exists("dataset/imagenet.yaml") assert not repo.config_exists("not_found.yaml") @pytest.mark.parametrize( "config_path,expected", [ pytest.param( "primary_config", [], id="no_defaults", ), pytest.param( "config_with_defaults_list", [GroupDefault(group="dataset", value="imagenet")], id="defaults_in_root", ), pytest.param( "configs_with_defaults_list/global_package", [GroupDefault(group="foo", value="bar")], id="configs_with_defaults_list/global_package", ), pytest.param( "configs_with_defaults_list/group_package", [GroupDefault(group="foo", value="bar")], id="configs_with_defaults_list/group_package", ), pytest.param( "configs_with_defaults_list/no_package", [GroupDefault(group="foo", value="bar")], id="configs_with_defaults_list/no_package", ), ], ) def test_config_repository_list( self, hydra_restore_singletons: Any, path: str, config_path: str, expected: List[InputDefault], ) -> None: Plugins.instance() config_search_path = create_config_search_path(path) repo = ConfigRepository(config_search_path=config_search_path) ret = repo.load_config(config_path) assert ret is not None assert ret.defaults_list == expected