From 7f21e1a0e72a4edb31164032e012f4da8ad3ebfe Mon Sep 17 00:00:00 2001 From: GdoongMathew Date: Wed, 17 Sep 2025 01:57:44 +0800 Subject: [PATCH 1/2] check the init args only when the given frames are in `__init__` method. --- src/lightning/pytorch/utilities/parsing.py | 2 +- tests/tests_pytorch/models/test_hparams.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/lightning/pytorch/utilities/parsing.py b/src/lightning/pytorch/utilities/parsing.py index 829cc7a994b93..64aa0209819ab 100644 --- a/src/lightning/pytorch/utilities/parsing.py +++ b/src/lightning/pytorch/utilities/parsing.py @@ -91,7 +91,7 @@ def get_init_args(frame: types.FrameType) -> dict[str, Any]: # pragma: no-cover def _get_init_args(frame: types.FrameType) -> tuple[Optional[Any], dict[str, Any]]: _, _, _, local_vars = inspect.getargvalues(frame) - if "__class__" not in local_vars: + if "__class__" not in local_vars or frame.f_code.co_name != "__init__": return None, {} cls = local_vars["__class__"] init_parameters = inspect.signature(cls.__init__).parameters diff --git a/tests/tests_pytorch/models/test_hparams.py b/tests/tests_pytorch/models/test_hparams.py index 92a07f0a3d05e..9b31d18aa9edb 100644 --- a/tests/tests_pytorch/models/test_hparams.py +++ b/tests/tests_pytorch/models/test_hparams.py @@ -341,6 +341,20 @@ def __init__(obj, *more_args, other_arg=300, **more_kwargs): obj.save_hyperparameters() +class _MetaType(type): + def __call__(cls, *args, **kwargs): + instance = super().__call__(*args, **kwargs) # Create the instance + if hasattr(instance, "_after_init"): + instance._after_init(**kwargs) # Call the method if defined + return instance + + +class MetaTypeBoringModel(CustomBoringModel, metaclass=_MetaType): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.save_hyperparameters() + + if _OMEGACONF_AVAILABLE: class DictConfSubClassBoringModel(SubClassBoringModel): @@ -365,6 +379,7 @@ class DictConfSubClassBoringModel: ... pytest.param(DictConfSubClassBoringModel, marks=RunIf(omegaconf=True)), BoringModelWithMixin, BoringModelWithMixinAndInit, + MetaTypeBoringModel, ], ) def test_collect_init_arguments(tmp_path, cls): From 5dd6ddda55d3ce8ba3c1a762cfe8f055e02b2130 Mon Sep 17 00:00:00 2001 From: jirka Date: Tue, 16 Sep 2025 20:07:04 +0200 Subject: [PATCH 2/2] chlog --- src/lightning/pytorch/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lightning/pytorch/CHANGELOG.md b/src/lightning/pytorch/CHANGELOG.md index b3ed00611c021..654ad68c9c578 100644 --- a/src/lightning/pytorch/CHANGELOG.md +++ b/src/lightning/pytorch/CHANGELOG.md @@ -49,6 +49,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Fixed preventing recursive symlink creation iwhen `save_last='link'` and `save_top_k=-1` ([#21186](https://github.com/Lightning-AI/pytorch-lightning/pull/21186)) +- Fixed check the init args only when the given frames are in `__init__` method ([#21227](https://github.com/Lightning-AI/pytorch-lightning/pull/21227)) + + --- ## [2.5.5] - 2025-09-05