Skip to content

Using config_path with subparsers does not seem to work #241

Open
@aliounis

Description

@aliounis

Describe the bug
I am attempting to use subparsers and the config_path option together, however, I cannot seem to get the config path to actually set the defaults. I have tried multiple "paths" in the json file for the config file, posted below, but non seem to work. I've tried to investigate myself and think that the issue might be because the subparser does not copy the defaults for the command attribute (which chooses the subparser).

To Reproduce


import logging

from simple_parsing import ArgumentParser, Serializable


@dataclass
class DoA(Serializable):
    a_config: str = "taco"
    """
    what does a want?
    """

    def execute(self):
        print(f'A says {self.a_config}')


@dataclass
class DoB(Serializable):
    b_config: str = "pie"
    """
    what does b want?
    """

    def execute(self):
        print(f'B says {self.b_config}')


@dataclass
class Subprogram(Serializable):

    command: DoA | DoB
    """
    Do A or B?
    """

    def execute(self):
        self.command.execute()

if __name__ == "__main__":

    parser = ArgumentParser(add_config_path_arg=True)

    parser.add_arguments(Subprogram, dest="sub")

    # logging.basicConfig(level=logging.DEBUG, 
    #                     format="%(filename)s:%(lineno)s:%(name)s:%(message)s")
    args = parser.parse_args()

    sub: Subprogram = args.sub

    with open('./mwe_config_out.json', 'w') as ofile:
        sub.dump_json(ofile)

    sub.execute()

Expected behavior
Running

python sp_mwe.py doa --config_path mwe_config.json

with sp_mew.py being the above source code and mwe_config.json containing either:

    "sub": {
        "command": {
            "doa": {
                "a_config": "taco cheese"
            },
            "dob": {
                "b_config": "apple pie"
            }
        }
    }
}

or

    "sub": {
        "command": {
                "a_config": "taco cheese",
                "b_config": "apple pie"
            }
        }
    }

(I don't care which format, either would be fine) should produce the output

A says taco cheese

Actual behavior
Instead, using either of the json formats we get the default output of

A says taco

Desktop (please complete the following information):

  • Version 0.1.0 (3122f6e)
  • Python version: 3.11.0

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