Skip to content

Usage of parse_known_args() before adding a dataclass containing subgroups results in error #353

@jonahsiekmannagility

Description

@jonahsiekmannagility

Describe the bug
Usage of parse_known_args() before adding a dataclass with add_arguments results in the following error when subsequently running parse_args().

To Reproduce

from dataclasses import dataclass
from simple_parsing import subgroups

@dataclass
class SubgroupBase:
    ...

@dataclass
class SubgroupMemberA(SubgroupBase):
    ...

@dataclass
class GroupPicker:
    subgroup_type: SubgroupBase = subgroups(
        {
            "a": SubgroupMemberA,
        },
        default_factory=SubgroupMemberA,
    )


if __name__ == "__main__":
    from simple_parsing import ArgumentParser

    parser = ArgumentParser()
    parser.add_argument("positional_argument", type=str)
   # Removing this line prevents the error
    args, _ = parser.parse_known_args()

    parser.add_arguments(GroupPicker, "type")
    # AttributeError: 'Namespace' object has no attribute 'type.subgroup_type'
    args = parser.parse_args()

Expected behavior
I expect parse_known_args and parse_args to be callable any number of times without throwing a stacktrace

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

$ python issue.py
  File "[my script]", line 29, in <module>
    args = parser.parse_args()
  File ".../.local/share/uv/python/cpython-3.10.17-linux-x86_64-gnu/lib/python3.10/argparse.py", line 1833, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File ".../.venv/lib/python3.10/site-packages/simple_parsing/parsing.py", line 361, in parse_known_args
    parsed_args = self._postprocessing(parsed_args)
  File ".../.venv/lib/python3.10/site-packages/simple_parsing/parsing.py", line 580, in _postprocessing
    self._remove_subgroups_from_namespace(parsed_args)
  File ".../.venv/lib/python3.10/site-packages/simple_parsing/parsing.py", line 790, in _remove_subgroups_from_namespace
    chosen_value = getattr(parsed_args, dest)
AttributeError: 'Namespace' object has no attribute 'type.subgroup_type'

Desktop (please complete the following information):

  • Version [e.g. 22]: Ubuntu 24.04, simple-parsing 0.1.7
  • Python version: 3.10.17

Additional context
Add any other context about the problem here.

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