Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with subgroups and config file #331

Open
farzadab opened this issue Oct 3, 2024 · 2 comments
Open

Bug with subgroups and config file #331

farzadab opened this issue Oct 3, 2024 · 2 comments

Comments

@farzadab
Copy link

farzadab commented Oct 3, 2024

Describe the bug
Subgroups work if parameters are provided as CLI args, but it fails if the same parameter is passed through a config file. See below:

To Reproduce

from typing import Union
import dataclasses
import simple_parsing

@dataclasses.dataclass
class ModelTypeA:
    model_a_param: str

@dataclasses.dataclass
class ModelTypeB:
    model_b_param: str

@dataclasses.dataclass
class TrainConfig:
    model_type: Union[ModelTypeA, ModelTypeB] = simple_parsing.subgroups(
        {"type_a": ModelTypeA, "type_b": ModelTypeB},
        default_factory=ModelTypeA,
        positional=False,
    )


# this works:
simple_parsing.parse(config_class=TrainConfig, args=['--model_a_param', 'test'])

# this doesn't work and throws an error
simple_parsing.parse(config_class=TrainConfig, add_config_path_arg=True, args=['--config_path', 'config.yaml'])

# config.yaml:
# model_a_param: test

Expected behavior
I expect the config file to be allow the same behaviour as the CLI args.

Actual behavior
As described above, the second invocation leads to the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/farzad/Library/Caches/pypoetry/virtualenvs/ultravox-o1nHeuex-py3.11/lib/python3.11/site-packages/simple_parsing/parsing.py", line 1030, in parse
    parsed_args = parser.parse_args(args)
                  ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/farzad/.pyenv/versions/3.11.4/lib/python3.11/argparse.py", line 1869, in parse_args
    args, argv = self.parse_known_args(args, namespace)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/farzad/Library/Caches/pypoetry/virtualenvs/ultravox-o1nHeuex-py3.11/lib/python3.11/site-packages/simple_parsing/parsing.py", line 321, in parse_known_args
    self.set_defaults(config_file)
  File "/Users/farzad/Library/Caches/pypoetry/virtualenvs/ultravox-o1nHeuex-py3.11/lib/python3.11/site-packages/simple_parsing/parsing.py", line 405, in set_defaults
    wrapper.set_default(default_for_dataclass)
  File "/Users/farzad/Library/Caches/pypoetry/virtualenvs/ultravox-o1nHeuex-py3.11/lib/python3.11/site-packages/simple_parsing/wrappers/dataclass_wrapper.py", line 313, in set_default
    raise RuntimeError(
RuntimeError: ['model_a_param'] are not fields of <class '__main__.TrainConfig'> at path 'config'!

Desktop (please complete the following information):

  • Simple parsing version: 0.1.6
  • Python version: 3.11.4
  • Machine: M1 Mac
@farzadab
Copy link
Author

farzadab commented Oct 3, 2024

@lebrice you mentioned on #306 that you are working on a refactor of subgroups. Have you gotten around to that yet?
I know very little about the workings of simple_parsing to propose any fix :)

@lebrice
Copy link
Owner

lebrice commented Oct 4, 2024

Hey @farzadab , thanks for posting!

#284 is the closest I've gotten to it, but no I haven't worked on this problem in a while, unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants