Skip to content

Commit 2af9944

Browse files
committed
fix implementation
1 parent 6a09f27 commit 2af9944

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/lightning/pytorch/utilities/parsing.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,14 @@ def save_hyperparameters(
167167
if given_hparams is not None:
168168
init_args = given_hparams
169169
elif is_dataclass(obj):
170-
init_args = {f.name: getattr(obj, f.name) for f in fields(obj)}
170+
obj_fields = fields(obj)
171+
init_args = {f.name: getattr(obj, f.name) for f in obj_fields if f.init}
172+
if any(not f.init for f in obj_fields):
173+
rank_zero_warn(
174+
"Detected a dataclass with fields with `init=False`. This is not supported by `save_hyperparameters`"
175+
" and will not save those fields. Consider removing `init=False` and just re-initialize the attributes"
176+
" in the `__post_init__` method of the dataclass."
177+
)
171178
else:
172179
init_args = {}
173180

0 commit comments

Comments
 (0)