openml / openml-python

Python module to interface with OpenML
https://openml.github.io/openml-python/main/
Other
276 stars 142 forks source link

Config file being overwritten at import #1337

Closed BrunoBelucci closed 2 months ago

BrunoBelucci commented 2 months ago

Description

The config file located at .../.config/openml/config is being overwritten with the default values when importing openml.

Steps/Code to Reproduce

1 - Create a config file in .../.config/openml/config 2 - import openml in any python script 3 - check that the file is now empty and the config values being used are the default ones

Expected Results

Do not overwrite the config file

Actual Results

Config file is overwritten

Versions

Linux-6.5.0-28-generic-x86_64-with-glibc2.35 Python 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) [GCC 12.3.0] NumPy 1.26.4 SciPy 1.13.0 Scikit-Learn 1.4.2 OpenML 0.14.2

Possible solution

I think that the reason is the following code under config.py

def _parse_config(config_file: str | Path) -> _Config:
    """Parse the config file, set up defaults."""
    config_file = Path(config_file)
    config = configparser.RawConfigParser(defaults=_defaults)  # type: ignore

    # The ConfigParser requires a [SECTION_HEADER], which we do not expect in our config file.
    # Cheat the ConfigParser module by adding a fake section header
    config_file_ = StringIO()
    config_file_.write("[FAKE_SECTION]\n")
    try:
        with config_file.open("w") as fh:
            for line in fh:
                config_file_.write(line)
    except FileNotFoundError:
        logger.info("No config file found at %s, using default configuration.", config_file)
    except OSError as e:
        logger.info("Error opening file %s: %s", config_file, e.args[0])
    config_file_.seek(0)
    config.read_file(config_file_)
    return dict(config.items("FAKE_SECTION"))

The problem is that the file is being opened in write mode 'w', when it should be open in read mode 'r'. I will see if I can provide a PR to fix it.