From 3a260f83838b87dc77d20500d39977b8aeb5e8bf Mon Sep 17 00:00:00 2001 From: Bruno Date: Sun, 12 May 2024 13:17:14 +0200 Subject: [PATCH 1/4] read file in read mode --- openml/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openml/config.py b/openml/config.py index 4744dbe86..1c4a4b8c7 100644 --- a/openml/config.py +++ b/openml/config.py @@ -319,7 +319,7 @@ def _parse_config(config_file: str | Path) -> _Config: config_file_ = StringIO() config_file_.write("[FAKE_SECTION]\n") try: - with config_file.open("w") as fh: + with config_file.open("r") as fh: for line in fh: config_file_.write(line) except FileNotFoundError: From 2912e1af0d5b8594e21bd21b75fc8bd4cb65fee4 Mon Sep 17 00:00:00 2001 From: Bruno Date: Sun, 12 May 2024 13:30:04 +0200 Subject: [PATCH 2/4] cast parameters to expected types --- openml/config.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openml/config.py b/openml/config.py index 1c4a4b8c7..942b2dba0 100644 --- a/openml/config.py +++ b/openml/config.py @@ -252,11 +252,11 @@ def _setup(config: _Config | None = None) -> None: if config is None: config = _parse_config(config_file) - avoid_duplicate_runs = config.get("avoid_duplicate_runs", False) + avoid_duplicate_runs = bool(config.get("avoid_duplicate_runs", False)) apikey = config["apikey"] server = config["server"] - short_cache_dir = config["cachedir"] - n_retries = config["connection_n_retries"] + short_cache_dir = Path(config["cachedir"]) + n_retries = int(config["connection_n_retries"]) set_retry_policy(config["retry_policy"], n_retries) From dbffcac3aa13d47270e6cb71e5505ace3ec5e2fe Mon Sep 17 00:00:00 2001 From: Bruno Date: Mon, 13 May 2024 16:05:21 +0200 Subject: [PATCH 3/4] Following PGijsbers proposal to ensure that avoid_duplicate_runs is a boolean after reading it from config_file --- openml/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openml/config.py b/openml/config.py index 942b2dba0..7449586a8 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 = bool(config.get("avoid_duplicate_runs", False)) + avoid_duplicate_runs = config.get("avoid_duplicate_runs", "").lower() == "true" apikey = config["apikey"] server = config["server"] short_cache_dir = Path(config["cachedir"]) From 4a9b5684749f7eff1ebd072d007ed73afbde6807 Mon Sep 17 00:00:00 2001 From: PGijsbers Date: Tue, 14 May 2024 09:39:17 +0200 Subject: [PATCH 4/4] Add a test, move parsing of avoid_duplicate_runs --- openml/config.py | 6 +++++- tests/test_openml/test_config.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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"]