Skip to content
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 @@ -28,6 +28,8 @@ Fixed
space (`#499 <https://github.com/omni-us/jsonargparse/pull/499>`__).
- ``format_usage()`` not working (`#501
<https://github.com/omni-us/jsonargparse/issues/501>`__).
- Not able to modify init args for callable with class return and default class
(`#5?? <https://github.com/omni-us/jsonargparse/pull/5??>`__).


v4.28.0 (2024-04-17)
Expand Down
2 changes: 2 additions & 0 deletions jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,8 @@ def subclass_spec_as_namespace(val, prev_val=None):
val = Namespace({root_key: val})
if isinstance(prev_val, str):
prev_val = Namespace(class_path=prev_val)
elif inspect.isclass(prev_val):
prev_val = Namespace(class_path=get_import_path(prev_val))
if isinstance(val, dict):
val = Namespace(val)
if "init_args" in val and isinstance(val["init_args"], dict):
Expand Down
7 changes: 7 additions & 0 deletions jsonargparse_tests/test_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,13 @@ def test_callable_multiple_args_return_type_class(parser, subtests):
assert f"{__name__}.{name}" in help_str


def test_callable_return_class_default_class_override_init_arg(parser):
parser.add_argument("--optimizer", type=Callable[[List[float]], Optimizer], default=SGD)
cfg = parser.parse_args(["--optimizer.momentum=0.5", "--optimizer.lr=0.05"])
assert cfg.optimizer.class_path == f"{__name__}.SGD"
assert cfg.optimizer.init_args == Namespace(lr=0.05, momentum=0.5)


class StepLR:
def __init__(self, optimizer: Optimizer, last_epoch: int = -1):
self.optimizer = optimizer
Expand Down