def test_load_history_with_basic_launcher(self, path: str) -> None:
        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path)
        )
        cfg = config_loader.load_configuration(
            config_name="custom_default_launcher.yaml",
            overrides=["hydra/launcher=basic"],
            strict=False,
            run_mode=RunMode.RUN,
        )

        expected = deepcopy(hydra_load_list)
        expected.append(
            LoadTrace(
                config_path="custom_default_launcher.yaml",
                package="",
                parent="<root>",
                search_path=path,
                provider="main",
            )
        )
        assert_same_composition_trace(cfg.hydra.composition_trace, expected)
Beispiel #2
0
    def test_sweep_config_cache(self, hydra_restore_singletons: Any, path: str,
                                monkeypatch: Any) -> None:
        setup_globals()

        monkeypatch.setenv("TEST_ENV", "test_env")

        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path))
        master_cfg = config_loader.load_configuration(
            config_name="config.yaml",
            overrides=[
                "+time=${now:%H-%M-%S}", "+test_env=${oc.env:TEST_ENV}"
            ],
            run_mode=RunMode.RUN,
        )

        # trigger resolution by type assertion
        assert type(master_cfg.time) == str
        assert type(master_cfg.test_env) == str

        master_cfg_cache = OmegaConf.get_cache(master_cfg)
        assert "now" in master_cfg_cache.keys()
        # oc.env is not cached as of OmegaConf 2.1
        assert "oc.env" not in master_cfg_cache.keys()
        assert master_cfg.test_env == "test_env"

        sweep_cfg = config_loader.load_sweep_config(
            master_config=master_cfg,
            sweep_overrides=[
                "+time=${now:%H-%M-%S}", "+test_env=${oc.env:TEST_ENV}"
            ],
        )

        sweep_cfg_cache = OmegaConf.get_cache(sweep_cfg)
        assert len(
            sweep_cfg_cache.keys()) == 1 and "now" in sweep_cfg_cache.keys()
        assert sweep_cfg_cache["now"] == master_cfg_cache["now"]
        monkeypatch.setenv("TEST_ENV", "test_env2")
        assert sweep_cfg.test_env == "test_env2"
    def test_load_history(self, path: str) -> None:
        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path)
        )
        cfg = config_loader.load_configuration(
            config_name="missing-optional-default.yaml",
            overrides=[],
            run_mode=RunMode.RUN,
        )
        expected = deepcopy(hydra_load_list)
        expected.append(
            LoadTrace(
                config_path="missing-optional-default.yaml",
                package="",
                parent="<root>",
                is_self=True,
                search_path=path,
                provider="main",
            )
        )

        assert_same_composition_trace(cfg.hydra.composition_trace, expected)
Beispiel #4
0
def test_load_schema_as_config(hydra_restore_singletons: Any) -> None:
    """
    Load structured config as a configuration
    """
    ConfigStore.instance().store(
        name="config", node=TopLevelConfig, provider="this_test"
    )

    ConfigStore.instance().store(
        name="db/mysql", node=MySQLConfig, provider="this_test"
    )

    config_loader = ConfigLoaderImpl(config_search_path=create_config_search_path(None))
    cfg = config_loader.load_configuration(
        config_name="config", overrides=[], run_mode=RunMode.RUN
    )

    expected = deepcopy(hydra_load_list)
    expected.extend(
        [
            LoadTrace(
                config_name="config", search_path="structured://", provider="this_test"
            )
        ]
    )
    assert_same_composition_trace(cfg.hydra.composition_trace, expected)

    with open_dict(cfg):
        del cfg["hydra"]
    assert cfg == {
        "normal_yaml_config": "???",
        "db": {
            "driver": MISSING,
            "host": MISSING,
            "port": MISSING,
            "user": MISSING,
            "password": MISSING,
        },
    }
Beispiel #5
0
def test_list_groups() -> None:
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path(
            "hydra/test_utils/configs/cloud_infra_example"))
    groups = config_loader.list_groups("")
    assert sorted(groups) == [
        "application",
        "cloud_provider",
        "db",
        "environment",
        "hydra",
    ]

    assert sorted(config_loader.list_groups("hydra")) == [
        "help",
        "hydra_help",
        "hydra_logging",
        "job_logging",
        "launcher",
        "output",
        "sweeper",
    ]
Beispiel #6
0
def test_default_removal(config_file: str, overrides: List[str]) -> None:
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path("hydra/test_utils/configs")
    )
    cfg = config_loader.load_configuration(
        config_name=config_file, overrides=overrides, strict=False, run_mode=RunMode.RUN
    )

    expected = deepcopy(hydra_load_list)
    for x in expected:
        if x.config_group == "hydra/launcher":
            x.skip_reason = "deleted_from_list"
            x.search_path = None
            x.provider = None
    expected.append(
        LoadTrace(
            config_name=config_file,
            search_path="file://hydra/test_utils/configs",
            provider="main",
        )
    )
    assert_same_composition_trace(cfg.hydra.composition_trace, expected)
def test_default_removal(config_file: str, overrides: List[str]) -> None:
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path("hydra/test_utils/configs")
    )
    cfg = config_loader.load_configuration(
        config_name=config_file, overrides=overrides, strict=False, run_mode=RunMode.RUN
    )

    expected = deepcopy(hydra_load_list)
    expected = list(
        filterfalse(lambda x: x.config_path == "hydra/launcher/basic", expected)
    )
    expected.append(
        LoadTrace(
            config_path=config_file,
            package="",
            parent="<root>",
            search_path="file://hydra/test_utils/configs",
            provider="main",
        )
    )
    assert_same_composition_trace(cfg.hydra.composition_trace, expected)
Beispiel #8
0
    def test_load_config_with_schema(self, restore_singletons: Any, path: str) -> None:

        ConfigStore.instance().store(
            group="db",
            name="mysql",
            node=MySQLConfig,
            path="db",
            provider="test_provider",
        )

        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path)
        )

        cfg = config_loader.load_configuration(config_name="db/mysql", overrides=[])
        del cfg["hydra"]
        assert cfg == {
            "db": {
                "driver": "mysql",
                "host": "???",
                "port": "???",
                "user": "******",
                "password": "******",
            }
        }

        expected = hydra_load_list.copy()
        expected.append(LoadTrace("db/mysql", path, "main", "test_provider"))
        assert config_loader.get_load_history() == expected

        # verify illegal modification is rejected at runtime
        with pytest.raises(ValidationError):
            cfg.db.port = "fail"

        # verify illegal override is rejected during load
        with pytest.raises(ValidationError):
            config_loader.load_configuration(
                config_name="db/mysql", overrides=["db.port=fail"]
            )
Beispiel #9
0
    def test_load_config_file_with_schema_validation(
        self, hydra_restore_singletons: Any, path: str
    ) -> None:

        with ConfigStoreWithProvider("this_test") as cs:
            cs.store(name="config", node=TopLevelConfig)
            cs.store(group="db", name="mysql", node=MySQLConfig, package="db")

        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path)
        )

        msg = dedent(
            """\
          'config' is validated against ConfigStore schema with the same name.
          This behavior is deprecated in Hydra 1.1 and will be removed in Hydra 1.2.
          See https://hydra.cc/docs/next/upgrades/1.0_to_1.1/automatic_schema_matching for migration instructions."""
        )
        with pytest.warns(UserWarning, match=re.escape(msg)):
            cfg = config_loader.load_configuration(
                config_name="config",
                overrides=["+db=mysql"],
                strict=False,
                run_mode=RunMode.RUN,
            )

        with open_dict(cfg):
            del cfg["hydra"]
        assert cfg == {
            "normal_yaml_config": True,
            "db": {
                "driver": "mysql",
                "host": "???",
                "port": "???",
                "user": "******",
                "password": "******",
            },
        }
Beispiel #10
0
def test_mixed_composition_order() -> None:
    """
    Tests that the order of mixed composition (defaults contains both config group and non config group
    items) is correct
    """
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path("hydra/test_utils/configs")
    )
    config_loader.load_configuration(
        config_name="mixed_compose.yaml", overrides=[], strict=False
    )

    expected = hydra_load_list.copy()
    expected.extend(
        [
            ("mixed_compose.yaml", "file://hydra/test_utils/configs", "main", None),
            ("some_config", "file://hydra/test_utils/configs", "main", None),
            ("group1/file1", "file://hydra/test_utils/configs", "main", None),
            ("config", "file://hydra/test_utils/configs", "main", None),
        ]
    )

    assert config_loader.get_load_history() == expected
Beispiel #11
0
def test_non_config_group_default() -> None:
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path(
            "hydra/test_utils/configs"))
    config_loader.load_configuration(
        config_name="non_config_group_default.yaml",
        overrides=[],
        strict=False,
        run_mode=RunMode.RUN,
    )

    expected = hydra_load_list.copy()
    expected.extend([
        LoadTrace(
            "non_config_group_default.yaml",
            "file://hydra/test_utils/configs",
            "main",
            None,
        ),
        LoadTrace("some_config", "file://hydra/test_utils/configs", "main",
                  None),
    ])
    assert config_loader.get_load_history() == expected
Beispiel #12
0
def test_overlapping_schemas(restore_singletons: Any) -> None:  # noqa: F811

    cs = ConfigStore.instance()
    cs.store(name="config", node=Config)
    cs.store(group="plugin",
             name="concrete",
             node=ConcretePlugin,
             path="plugin")

    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path(None))
    cfg = config_loader.load_configuration(config_name="config", overrides=[])
    del cfg["hydra"]
    assert cfg == {"plugin": {"name": "???", "params": "???"}}
    assert cfg.plugin._type == Plugin

    cfg = config_loader.load_configuration(config_name="config",
                                           overrides=["plugin=concrete"])
    del cfg["hydra"]
    assert cfg == {"plugin": {"name": "foobar_plugin", "params": {"foo": 10}}}
    assert cfg.plugin._type == ConcretePlugin
    assert cfg.plugin.params._type == ConcretePlugin.FoobarParams
    with pytest.raises(ValidationError):
        cfg.plugin = 10
Beispiel #13
0
def test_load_schema_as_config(restore_singletons: Any) -> None:  # noqa: F811
    """
    Load structured config as a configuration
    """
    ConfigStore.instance().store(
        group="db", name="mysql", node=MySQLConfig, path="db", provider="test_provider"
    )

    config_loader = ConfigLoaderImpl(config_search_path=create_config_search_path(None))
    cfg = config_loader.load_configuration(config_name="db/mysql", overrides=[])
    del cfg["hydra"]
    assert cfg == {
        "db": {
            "driver": MISSING,
            "host": MISSING,
            "port": MISSING,
            "user": MISSING,
            "password": MISSING,
        }
    }

    expected = hydra_load_list.copy()
    expected.extend([("db/mysql", "structured://", "test_provider", None)])
    assert config_loader.get_load_history() == expected
Beispiel #14
0
    def test_load_strict(self, path: str) -> None:
        """
        Ensure that in strict mode we can override only things that are already in the config
        :return:
        """
        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path))
        # Test that overriding existing things works in strict mode
        cfg = config_loader.load_configuration(config_name="compose.yaml",
                                               overrides=["foo=ZZZ"],
                                               strict=True)
        del cfg["hydra"]
        assert cfg == {"foo": "ZZZ", "bar": 100}

        # Test that accessing a key that is not there will fail
        with pytest.raises(AttributeError):
            # noinspection PyStatementEffect
            cfg.not_here

        # Test that bad overrides triggers the KeyError
        with pytest.raises(AttributeError):
            config_loader.load_configuration(config_name="compose.yaml",
                                             overrides=["f00=ZZZ"],
                                             strict=True)
Beispiel #15
0
def test_setup_plugins(
    monkeypatch: Any, plugin: Union[Launcher, Sweeper], config: DictConfig
) -> None:
    task_function = Mock(spec=TaskFunction)
    config_loader = ConfigLoaderImpl(config_search_path=create_config_search_path(None))
    hydra_context = HydraContext(config_loader=config_loader, callbacks=Callbacks())
    plugin_instance = Plugins.instance()
    monkeypatch.setattr(Plugins, "check_usage", lambda _: None)
    monkeypatch.setattr(plugin_instance, "_instantiate", lambda _: plugin)

    msg = "setup() got an unexpected keyword argument 'hydra_context'"
    with raises(TypeError, match=re.escape(msg)):
        if isinstance(plugin, Launcher):
            Plugins.instance().instantiate_launcher(
                hydra_context=hydra_context,
                task_function=task_function,
                config=config,
            )
        else:
            Plugins.instance().instantiate_sweeper(
                hydra_context=hydra_context,
                task_function=task_function,
                config=config,
            )
Beispiel #16
0
    def test_load_config_with_schema(
        self, hydra_restore_singletons: Any, path: str
    ) -> None:

        ConfigStore.instance().store(
            name="config", node=TopLevelConfig, provider="this_test"
        )
        ConfigStore.instance().store(
            group="db", name="mysql", node=MySQLConfig, provider="this_test"
        )

        config_loader = ConfigLoaderImpl(
            config_search_path=create_config_search_path(path)
        )

        cfg = config_loader.load_configuration(
            config_name="config", overrides=["+db=mysql"], run_mode=RunMode.RUN
        )

        expected = deepcopy(hydra_load_list)
        expected.append(
            LoadTrace(
                config_path="config",
                package="",
                parent="<root>",
                is_self=False,
                search_path=path,
                provider="main",
            )
        )
        expected.append(
            LoadTrace(
                config_path="db/mysql",
                package="db",
                parent="<root>",
                is_self=False,
                search_path=path,
                provider="main",
            )
        )
        assert_same_composition_trace(cfg.hydra.composition_trace, expected)

        with open_dict(cfg):
            del cfg["hydra"]
        assert cfg == {
            "normal_yaml_config": True,
            "db": {
                "driver": "mysql",
                "host": "???",
                "port": "???",
                "user": "******",
                "password": "******",
            },
        }

        # verify illegal modification is rejected at runtime
        with pytest.raises(ValidationError):
            cfg.db.port = "fail"

        # verify illegal override is rejected during load
        with pytest.raises(HydraException):
            config_loader.load_configuration(
                config_name="db/mysql", overrides=["db.port=fail"], run_mode=RunMode.RUN
            )
Beispiel #17
0
 def test_load_with_missing_default(self, path: str) -> None:
     config_loader = ConfigLoaderImpl(
         config_search_path=create_config_search_path(path))
     with pytest.raises(MissingConfigException):
         config_loader.load_configuration(
             config_name="missing-default.yaml", overrides=[], strict=False)
Beispiel #18
0
 def test_change_run_dir_with_config(self, path: str) -> None:
     config_loader = ConfigLoaderImpl(
         config_search_path=create_config_search_path(path))
     cfg = config_loader.load_configuration(
         config_name="overriding_run_dir.yaml", overrides=[], strict=False)
     assert cfg.hydra.run.dir == "cde"
Beispiel #19
0
def create_config_loader() -> ConfigLoaderImpl:
    return ConfigLoaderImpl(config_search_path=create_config_search_path(
        search_path_dir=os.path.realpath(
            "hydra/test_utils/configs/completion_test")))
Beispiel #20
0
def test_override_hydra_config_group_from_config_file() -> None:
    config_loader = ConfigLoaderImpl(
        config_search_path=create_config_search_path("hydra/test_utils/configs")
    )

    cfg = config_loader.load_configuration(
        config_name="overriding_logging_default.yaml",
        overrides=[],
        strict=False,
        run_mode=RunMode.RUN,
    )
    expected = [
        LoadTrace(
            config_path="hydra/output/default",
            package="hydra",
            parent="hydra/config",
            search_path="pkg://hydra.conf",
            provider="hydra",
        ),
        LoadTrace(
            config_path="hydra/launcher/basic",
            package="hydra.launcher",
            parent="hydra/config",
            search_path="structured://",
            provider="hydra",
        ),
        LoadTrace(
            config_path="hydra/sweeper/basic",
            package="hydra.sweeper",
            parent="hydra/config",
            search_path="structured://",
            provider="hydra",
        ),
        LoadTrace(
            config_path="hydra/help/default",
            package="hydra.help",
            parent="hydra/config",
            search_path="pkg://hydra.conf",
            provider="hydra",
        ),
        LoadTrace(
            config_path="hydra/hydra_help/default",
            package="hydra.hydra_help",
            parent="hydra/config",
            search_path="pkg://hydra.conf",
            provider="hydra",
        ),
        LoadTrace(
            config_path="hydra/hydra_logging/hydra_debug",
            package="hydra.hydra_logging",
            parent="hydra/config",
            search_path="pkg://hydra.conf",
            provider="hydra",
        ),
        LoadTrace(
            config_path="hydra/job_logging/disabled",
            package="hydra.job_logging",
            parent="hydra/config",
            search_path="pkg://hydra.conf",
            provider="hydra",
        ),
        LoadTrace(
            config_path="hydra/config",
            package="hydra",
            parent="<root>",
            is_self=True,
            search_path="structured://",
            provider="hydra",
        ),
        LoadTrace(
            config_path="overriding_logging_default.yaml",
            package="",
            parent="<root>",
            search_path="file://hydra/test_utils/configs",
            provider="main",
        ),
    ]

    assert_same_composition_trace(cfg.hydra.composition_trace, expected)
Beispiel #21
0
def config_loader() -> Any:
    return ConfigLoaderImpl(config_search_path=create_config_search_path(
        "pkg://hydra.test_utils.configs"))