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 lazy_instance not working for callable classes #487

Merged
merged 3 commits into from
Apr 15, 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 @@ -29,6 +29,8 @@ Fixed
- Account for breaking change in ``argparse.ArgumentParser._parse_optional``
affecting python ``3.11.9`` and likely ``>3.13`` (`#484
<https://github.com/omni-us/jsonargparse/issues/484>`__).
- ``lazy_instance`` not working for callable classes (`#473 comment
<https://github.com/omni-us/jsonargparse/issues/481#issuecomment-2030932435>`__).


v4.27.7 (2024-03-21)
Expand Down
12 changes: 9 additions & 3 deletions jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@ class LazyInitBaseClass:
def __init__(self, class_type: Type, lazy_kwargs: dict):
assert not issubclass(class_type, LazyInitBaseClass)
check_lazy_kwargs(class_type, lazy_kwargs)
self._lazy = type(self)
self._lazy_class_type = class_type
self._lazy_kwargs = lazy_kwargs
self._lazy_methods = {}
Expand All @@ -1308,17 +1309,22 @@ def __init__(self, class_type: Type, lazy_kwargs: dict):
if id(member) in seen_methods:
self.__dict__[name] = seen_methods[id(member)]
else:
self.__dict__[name] = partial(self._lazy_init_then_call_method, name)
seen_methods[id(member)] = self.__dict__[name]
lazy_method = partial(self._lazy_init_then_call_method, name)
self.__dict__[name] = lazy_method
if name == "__call__":
self._lazy.__call__ = lazy_method # type: ignore[method-assign]
seen_methods[id(member)] = lazy_method

def _lazy_init(self):
for name in self._lazy_methods:
if name == "__call__":
self._lazy.__call__ = self._lazy_methods[name]
del self.__dict__[name]
super().__init__(**self._lazy_kwargs)

def _lazy_init_then_call_method(self, method_name, *args, **kwargs):
self._lazy_init()
return getattr(self, method_name)(*args, **kwargs)
return self._lazy_methods[method_name](*args, **kwargs)

def lazy_get_init_args(self) -> Namespace:
return Namespace(self._lazy_kwargs)
Expand Down
15 changes: 15 additions & 0 deletions jsonargparse_tests/test_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,21 @@ def test_lazy_instance_pickleable():
assert reloaded.lazy_get_init_data() == instance1.lazy_get_init_data()


class OptimizerCallable:
def __init__(self, lr: float = 0.1):
self.lr = lr

def __call__(self, params) -> SGD:
return SGD(params, lr=self.lr)


def test_lazy_instance_callable():
lazy_optimizer = lazy_instance(OptimizerCallable, lr=0.2)
optimizer = lazy_optimizer([1, 2])
assert optimizer.lr == 0.2
assert optimizer.params == [1, 2]


# other tests


Expand Down
Loading