Skip to content

Use the parent's default for cmd=False fields of a nested dataclass - #382

Open
mayuriphad wants to merge 2 commits into
lebrice:masterfrom
mayuriphad:fix/371-cmd-false-parent-default
Open

mayuriphad wants to merge 2 commits into
lebrice:masterfrom
mayuriphad:fix/371-cmd-false-parent-default

Conversation

@mayuriphad

@mayuriphad mayuriphad commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #371.

DataclassWrapper gives cmd=False fields no argument and leaves them out of the constructor arguments. When a nested dataclass's default instance came from the parent, for example default_factory=lambda: Inner(a=1, b=[1, 2, 3]), those fields fell back to the field's own default, so the parent's value was silently lost:

simple_parsing.parse(Outer, args="")  # before: Outer(inner=Inner(a=1, b=[]))
                                      # after:  Outer(inner=Inner(a=1, b=[1, 2, 3]))

_create_dataclass_instance now fills in each init=True, cmd=False field that is missing from the constructor arguments, taking the value from the wrapper's default. The default can be an instance of the same dataclass or a dict. The value is deep-copied so the parsed object doesn't share mutable values with the default. A default= passed to parse() had the same problem and is fixed too. Nothing changes when there is no single default, e.g. with ConflictResolution.ALWAYS_MERGE or a default of another class.

Tests: three new cases in test/test_fields.py cover the nested case from the issue, with and without a CLI value, and a default= passed to parse(). All three fail on master. The full suite gives the same 8 failures on master and on this branch, plus 3 new passes. The ruff, ruff-format, whitespace and codespell hooks pass.

DataclassWrapper skips cmd=False fields, so they never reach the
constructor arguments and the nested dataclass fell back to the field's
own default, silently dropping the value from the parent's default
instance (or from a default passed to the parser). Take those values
from the wrapper's default instance, copied so the parsed object does
not share mutable values with it.

Fixes lebrice#371

Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 12, 2026 15:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

docformatter capitalizes the first word of a summary, which turned
"cmd=False" into "Cmd=False".

Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
@mayuriphad

Copy link
Copy Markdown
Contributor Author

The red build jobs come from test/test_bools.py::test_bool_args_in_help, which fails in the same way on master (e.g. the run for be6e4d6). This PR doesn't touch it. The help text assertion looks sensitive to the argparse output of newer Python patch releases.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

cmd=False fields of a nested dataclass ignore the parent's default instance

2 participants