SimpleParsing icon indicating copy to clipboard operation
SimpleParsing copied to clipboard

Default from config file not working for List of enum

Open hugobb opened this issue 2 years ago • 1 comments

Describe the bug Using a config file to load default is not working when the config contains a list of enum as a field.

To Reproduce

from dataclasses import dataclass, field
from enum import Enum
import tempfile
from typing import List
import simple_parsing


class TestEnum(Enum):
    A = "a"
    B = "b"


@dataclass
class TestConfig(simple_parsing.Serializable):
    enum_list: List[TestEnum] = field(default_factory=lambda: [TestEnum.A])


if __name__ == "__main__":
    with tempfile.NamedTemporaryFile(suffix=".yaml") as fp:
        config = TestConfig()
        config.save(fp.name)

        cfg = simple_parsing.parse(
            config_class=TestConfig,
            args=f"--config_path {fp.name}",
            add_config_path_arg=True,
        )
        print(cfg)
        assert cfg.enum_list == [TestEnum.A]

Expected behavior The assertion should pass: the enum_list field should be converted to a list of TestEnum.

$ python test.py
TestConfig(enum_list=[TestEnum.A])

Actual behavior The assertion fails: the enum_list field is not converted to a list of TestEnum

$ python test.py
TestConfig(enum_list=['A'])
Traceback (most recent call last):
  File "test.py", line 29, in <module>
    assert cfg.enum_list == [TestEnum.A]
AssertionError

Desktop (please complete the following information):

  • Simple Parsing version: 0.0.21.post1
  • Python version: 3.10.8

Additional context No other contexts

hugobb avatar Jan 17 '23 00:01 hugobb

Thanks for posting @hugobb ! This is a nice small bug. I'll take a look.

lebrice avatar Jan 17 '23 08:01 lebrice