Skip to content

Using subgroups with config_path leads to a TypeError #276

Open
@anivegesana

Description

@anivegesana

Describe the bug
When using subgroups with config_path, a type error occurs when trying to resolve which subgroup the config file should belong to. Adding a _type_ or subgroup key attribute automatically to all such entries and instantiating dictionaries that are assigned to subgroups inside of config files is a potential fix.

To Reproduce

from dataclasses import dataclass
from simple_parsing import subgroups, ArgumentParser
from simple_parsing.wrappers.field_wrapper import (
    ArgumentGenerationMode,
    DashVariant,
    NestedMode,
)

@dataclass
class A:
    a: int = 0

@dataclass
class B1(A):
    b: float = 1

@dataclass
class B2(A):
    b: float = 2

@dataclass
class C:
    c: A = subgroups(
        {
            "b1": B1,
            "b2": B2,
        },
        default="b1"
    )

parser = ArgumentParser(
    argument_generation_mode=ArgumentGenerationMode.NESTED,
    nested_mode=NestedMode.WITHOUT_ROOT,
    add_option_string_dash_variants=DashVariant.UNDERSCORE_AND_DASH,
    config_path="config.yaml",
)
parser.add_arguments(C, dest="config")
args = parser.parse_args()
print(args)
c:
  _type_: __main__.B2
  a: 0
  b: 1

Expected behavior
The subgroup entries to be read into the ArgumentParser and able to be modified via command line.

$ python run.py
C(c=B2(a=0,b=1))
$ python run.py --c.a=1
C(c=B2(a=1,b=1))
$ python run.py --c=b1
C(c=B1(a=0,b=1))

Actual behavior
A clear and concise description of what is happening.

$ python run.py
Traceback (most recent call last):
  File "run.py", line 39, in <module>
    args = parser.parse_args()
  File "/home/anirudhvegesana/anaconda3/envs/py38/lib/python3.8/argparse.py", line 1768, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/home/anirudhvegesana/anaconda3/envs/py38/lib/python3.8/site-packages/simple_parsing/parsing.py", line 333, in parse_known_args
    self._preprocessing(args=args, namespace=namespace)
  File "/home/anirudhvegesana/anaconda3/envs/py38/lib/python3.8/site-packages/simple_parsing/parsing.py", line 523, in _preprocessing
    wrapped_dataclasses, chosen_subgroups = self._resolve_subgroups(
  File "/home/anirudhvegesana/anaconda3/envs/py38/lib/python3.8/site-packages/simple_parsing/parsing.py", line 703, in _resolve_subgroups
    dataclass_type = subgroup_field.field.metadata["subgroup_dataclass_types"][
TypeError: unhashable type: 'dict'

Desktop (please complete the following information):

  • Version: 0.1.3
  • Python version: 3.8.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions