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

Fix nested dataclass with init=False not working correctly #650

Merged
merged 2 commits into from
Dec 23, 2024
Merged
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
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
Loading