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

[#241] fixes defaults from the config path were not being passed #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions simple_parsing/wrappers/dataclass_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,15 @@ def defaults(self) -> list[DataclassT | dict[str, Any] | None | Literal[argparse
self._defaults = []
for default in self.parent.defaults:
if default not in (None, argparse.SUPPRESS):
default = getattr(default, self.name)
self._defaults.append(default)
# we need to check here if the default has been provided.
# If not we'll use the default_value option function
if hasattr(default, self.name):
default = getattr(default, self.name)
else:
default = utils.default_value(self._field)
if default is MISSING:
continue
self._defaults.append(default)
else:
default_field_value = utils.default_value(self._field)
if default_field_value is MISSING:
Expand Down
7 changes: 6 additions & 1 deletion simple_parsing/wrappers/field_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,12 @@ def add_subparsers(self, parser: ArgumentParser):
# Just for typing correctness, as we didn't explicitly change
# the return type of subparsers.add_parser method.)
subparser = cast("ArgumentParser", subparser)
subparser.add_arguments(dataclass_type, dest=self.dest)
# we need to propagate the defaults down to the sub dataclass if they've been set.
# there may need to be some error handling here in case the use has specified the wrong values for the default.
if isinstance(self.default, dict) and self.default.get(subcommand, None) is not None:
subparser.add_arguments(dataclass_type, dest=self.dest, default=dataclass_type(**self.default[subcommand]))
else:
subparser.add_arguments(dataclass_type, dest=self.dest)

def equivalent_argparse_code(self):
arg_options = self.arg_options.copy()
Expand Down