Repository navigation
Use the parent's default for cmd=False fields of a nested dataclass - #382
Open
mayuriphad wants to merge 2 commits into
Open
mayuriphad wants to merge 2 commits into
mayuriphad wants to merge 2 commits into
Conversation
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>
docformatter capitalizes the first word of a summary, which turned "cmd=False" into "Cmd=False". Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
Contributor
Author
|
The red |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #371.
DataclassWrappergivescmd=Falsefields no argument and leaves them out of the constructor arguments. When a nested dataclass's default instance came from the parent, for exampledefault_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:_create_dataclass_instancenow fills in eachinit=True,cmd=Falsefield 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. Adefault=passed toparse()had the same problem and is fixed too. Nothing changes when there is no single default, e.g. withConflictResolution.ALWAYS_MERGEor a default of another class.Tests: three new cases in
test/test_fields.pycover the nested case from the issue, with and without a CLI value, and adefault=passed toparse(). All three fail onmaster. The full suite gives the same 8 failures onmasterand on this branch, plus 3 new passes. Theruff,ruff-format, whitespace andcodespellhooks pass.