Skip to content

Commit

Permalink
Fix nested dataclass with init=False not working correctly (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
mauvilsa authored Dec 23, 2024
1 parent 9d55863 commit c5fbd11
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Fixed
- Adding ``attrs.define`` dataclasses with nested dataclasses that are marked with
``attrs.field`` (such as for a default factory) are not parsed correctly (`#643
<https://github.com/omni-us/jsonargparse/pull/643>`__)
- Nested dataclass with ``init=False`` not working correctly (`#650
<https://github.com/omni-us/jsonargparse/pull/650>`__).


v4.35.0 (2024-12-16)
Expand Down
11 changes: 8 additions & 3 deletions jsonargparse/_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ def add_class_arguments(
defaults = default.lazy_get_init_args().as_dict()
elif is_dataclass_like(default.__class__):
defaults = dataclass_to_dict(default)
args = set(k[len(prefix) :] for k in added_args)
skip_not_added = [k for k in defaults if k not in args]
if skip_not_added:
skip.update(skip_not_added) # skip init=False
elif isinstance(default, Namespace):
defaults = default.as_dict()
if defaults:
Expand Down Expand Up @@ -428,9 +432,10 @@ def _add_signature_parameter(
kwargs.update(sub_add_kwargs)
with ActionTypeHint.allow_default_instance_context():
action = container.add_argument(*args, **kwargs)
action.sub_add_kwargs = sub_add_kwargs
if is_subclass_typehint and len(subclass_skip) > 0:
action.sub_add_kwargs["skip"] = subclass_skip
if action is not None: # None when class without any parameters
action.sub_add_kwargs = sub_add_kwargs
if is_subclass_typehint and len(subclass_skip) > 0:
action.sub_add_kwargs["skip"] = subclass_skip
added_args.append(dest)
elif is_required and fail_untyped:
raise ValueError(
Expand Down
21 changes: 21 additions & 0 deletions jsonargparse_tests/test_dataclass_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,27 @@ def test_dataclass_field_init_false(parser):
assert isinstance(init.data, DataInitFalse)


@dataclasses.dataclass
class NestedDataInitFalse:
x: bool = dataclasses.field(default=False, init=False)


@dataclasses.dataclass
class ParentDataInitFalse:
y: NestedDataInitFalse = dataclasses.field(default_factory=NestedDataInitFalse)


def test_nested_dataclass_field_init_false(parser):
parser.add_class_arguments(ParentDataInitFalse, "data")
assert parser.get_defaults() == Namespace()
cfg = parser.parse_args([])
assert cfg == Namespace()
init = parser.instantiate_classes(cfg)
assert isinstance(init.data, ParentDataInitFalse)
assert isinstance(init.data.y, NestedDataInitFalse)
assert init.data.y.x is False


@dataclasses.dataclass
class DataFieldFactory:
a1: List[int] = dataclasses.field(default_factory=lambda: [1, 2, 3])
Expand Down

0 comments on commit c5fbd11

Please sign in to comment.