From 8357bb759b09105b4bc596dc69590a8407db27a4 Mon Sep 17 00:00:00 2001 From: euxhenh Date: Fri, 6 Jun 2025 22:08:33 +0000 Subject: [PATCH 1/5] Switching from locally defined function in _typehints.adapt_class_type to functools.partial to support pickleability --- jsonargparse/_typehints.py | 10 +++++----- jsonargparse_tests/test_typehints.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/jsonargparse/_typehints.py b/jsonargparse/_typehints.py index c093eab5..4a49264f 100644 --- a/jsonargparse/_typehints.py +++ b/jsonargparse/_typehints.py @@ -1424,11 +1424,11 @@ def adapt_class_type( instantiator_fn = get_class_instantiator() if partial_classes: - - def partial_instance(*args): - return instantiator_fn(val_class, *args, **{**init_args, **dict_kwargs}) - - return partial_instance + return partial( + instantiator_fn, + val_class, + **{**init_args, **dict_kwargs}, + ) return instantiator_fn(val_class, **{**init_args, **dict_kwargs}) prev_init_args = prev_val.get("init_args") if isinstance(prev_val, Namespace) else None diff --git a/jsonargparse_tests/test_typehints.py b/jsonargparse_tests/test_typehints.py index 0e2ac72a..d7fb2c2e 100644 --- a/jsonargparse_tests/test_typehints.py +++ b/jsonargparse_tests/test_typehints.py @@ -1239,6 +1239,21 @@ def test_callable_args_return_type_class_subconfig(parser, tmp_cwd): assert optimizer.momentum == 0.8 +def test_callable_args_pickleable(parser, tmp_cwd): + config = { + "class_path": "Adam", + "init_args": {"momentum": 0.8}, + } + Path("optimizer.yaml").write_text(json_or_yaml_dump(config)) + parser.add_class_arguments(CallableSubconfig, "m", sub_configs=True) + cfg = parser.parse_args(["--m.o=optimizer.yaml"]) + init = parser.instantiate_classes(cfg) + + filepath = str(tmp_cwd) + "/pickled.pkl" + with open(filepath, "wb") as f: + pickle.dump(init, f) + + class Module: pass From e68b56e8d971ceb3f7cd0d3a14e89108ce3bfa53 Mon Sep 17 00:00:00 2001 From: euxhenh Date: Fri, 6 Jun 2025 22:16:59 +0000 Subject: [PATCH 2/5] Add partial wrapper fix Changelog --- CHANGELOG.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d5a91c85..8e886dcb 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,6 +11,14 @@ The semantic versioning only considers the public API as described in :ref:`api-ref`. Components not mentioned in :ref:`api-ref` or different import paths are considered internals and can change in minor and patch releases. +v4.40.1 (2025-06-??) +-------------------- + +Fixed +^^^^^ +- ``adapt_class_type`` used a locally defined `partial_instance` wrapper + function that is not pickleable. + v4.40.1 (2025-05-??) -------------------- From 19d571c952f65ecb2f03033630e9a766a71d4f65 Mon Sep 17 00:00:00 2001 From: euxhenh Date: Fri, 6 Jun 2025 22:17:20 +0000 Subject: [PATCH 3/5] Fixed version --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8e886dcb..eb71298a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,7 +11,7 @@ The semantic versioning only considers the public API as described in :ref:`api-ref`. Components not mentioned in :ref:`api-ref` or different import paths are considered internals and can change in minor and patch releases. -v4.40.1 (2025-06-??) +v4.40.2 (2025-06-??) -------------------- Fixed From ed8514849da7a4be9b30d8d2e6ca85ad908b0c2f Mon Sep 17 00:00:00 2001 From: Euxhen Hasanaj Date: Mon, 9 Jun 2025 09:51:12 -0700 Subject: [PATCH 4/5] Update CHANGELOG.rst Co-authored-by: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> --- CHANGELOG.rst | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index eb71298a..80a0c159 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,14 +11,9 @@ The semantic versioning only considers the public API as described in :ref:`api-ref`. Components not mentioned in :ref:`api-ref` or different import paths are considered internals and can change in minor and patch releases. -v4.40.2 (2025-06-??) --------------------- - -Fixed -^^^^^ - ``adapt_class_type`` used a locally defined `partial_instance` wrapper - function that is not pickleable. - + function that is not pickleable (`#728 +`__). v4.40.1 (2025-05-??) -------------------- From c19551bb372a7d664991d64dd3cce410549b5ca5 Mon Sep 17 00:00:00 2001 From: euxhenh Date: Mon, 9 Jun 2025 16:53:22 +0000 Subject: [PATCH 5/5] Update changelog --- CHANGELOG.rst | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index eb71298a..1fc5056b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,15 +11,6 @@ The semantic versioning only considers the public API as described in :ref:`api-ref`. Components not mentioned in :ref:`api-ref` or different import paths are considered internals and can change in minor and patch releases. -v4.40.2 (2025-06-??) --------------------- - -Fixed -^^^^^ -- ``adapt_class_type`` used a locally defined `partial_instance` wrapper - function that is not pickleable. - - v4.40.1 (2025-05-??) -------------------- @@ -27,6 +18,9 @@ Fixed ^^^^^ - ``print_shtab`` incorrectly parsed from environment variable (`#725 `__). +- ``adapt_class_type`` used a locally defined `partial_instance` wrapper + function that is not pickleable (`#728 + `__). v4.40.0 (2025-05-16)