diff --git a/openml/config.py b/openml/config.py index 7449586a8..1af8a7456 100644 --- a/openml/config.py +++ b/openml/config.py @@ -252,7 +252,7 @@ def _setup(config: _Config | None = None) -> None: if config is None: config = _parse_config(config_file) - avoid_duplicate_runs = config.get("avoid_duplicate_runs", "").lower() == "true" + avoid_duplicate_runs = config["avoid_duplicate_runs"] apikey = config["apikey"] server = config["server"] short_cache_dir = Path(config["cachedir"]) @@ -328,6 +328,10 @@ def _parse_config(config_file: str | Path) -> _Config: logger.info("Error opening file %s: %s", config_file, e.args[0]) config_file_.seek(0) config.read_file(config_file_) + if isinstance(config["FAKE_SECTION"]["avoid_duplicate_runs"], str): + config["FAKE_SECTION"]["avoid_duplicate_runs"] = config["FAKE_SECTION"].getboolean( + "avoid_duplicate_runs" + ) # type: ignore return dict(config.items("FAKE_SECTION")) # type: ignore diff --git a/tests/test_openml/test_config.py b/tests/test_openml/test_config.py index bfb88a5db..67d2ce895 100644 --- a/tests/test_openml/test_config.py +++ b/tests/test_openml/test_config.py @@ -116,3 +116,20 @@ def test_example_configuration_start_twice(self): assert openml.config.apikey == "610344db6388d9ba34f6db45a3cf71de" assert openml.config.server == self.production_server + + +def test_configuration_file_not_overwritten_on_load(): + """ Regression test for #1337 """ + config_file_content = "apikey = abcd" + with tempfile.TemporaryDirectory() as tmpdir: + config_file_path = Path(tmpdir) / "config" + with config_file_path.open("w") as config_file: + config_file.write(config_file_content) + + read_config = openml.config._parse_config(config_file_path) + + with config_file_path.open("r") as config_file: + new_file_content = config_file.read() + + assert config_file_content == new_file_content + assert "abcd" == read_config["apikey"]