From ee2362d44c91fb2fe0d58de67806569f12d9d296 Mon Sep 17 00:00:00 2001 From: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> Date: Mon, 20 Oct 2025 07:25:09 +0200 Subject: [PATCH] Fix Union with path type and non-path value incorrectly dumped as string. --- CHANGELOG.rst | 6 +++++- jsonargparse/typing.py | 2 ++ jsonargparse_tests/test_typehints.py | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e9e2b306..8e365502 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,14 +11,18 @@ 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.42.1 (unreleased) -------------------- Fixed ^^^^^ - Prevent extra environment variables in helptext when default_env=True, for - version actions and subcommands(`#787 + version actions and subcommands (`#787 `__). +- ``Union`` with path type and non-path value incorrectly dumped as string + (`#789 `__). + v4.42.0 (2025-10-14) -------------------- diff --git a/jsonargparse/typing.py b/jsonargparse/typing.py index 1708c684..05704d14 100644 --- a/jsonargparse/typing.py +++ b/jsonargparse/typing.py @@ -220,6 +220,8 @@ def _is_path_type(value, type_class): def _serialize_path(path: Path): + if not isinstance(path, Path): + raise ValueError("Expected a Path instance.") if path_dump_preserve_relative.get() and path.relative != path.absolute: return { "relative": path._relative, diff --git a/jsonargparse_tests/test_typehints.py b/jsonargparse_tests/test_typehints.py index 37e97412..aabcebd7 100644 --- a/jsonargparse_tests/test_typehints.py +++ b/jsonargparse_tests/test_typehints.py @@ -1590,3 +1590,9 @@ def mocked_get_import_path(cls): subclass_paths = get_all_subclass_paths(ImportClass) assert "Failed to import ImportClass" in str(w[0].message) assert subclass_paths == [] + + +def test_non_path_dump(parser): + parser.add_argument("--data", type=Union[Path_fr, Dict[str, List[str]]]) + cfg = parser.parse_args(['--data={"key": ["value"]}']) + assert json_or_yaml_load(parser.dump(cfg)) == {"data": {"key": ["value"]}}