Skip to content

Commit 7b1bbcd

Browse files
authored
Fix Union with path type and non-path value incorrectly dumped as string (#789)
1 parent 1f901fe commit 7b1bbcd

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ The semantic versioning only considers the public API as described in
1111
:ref:`api-ref`. Components not mentioned in :ref:`api-ref` or different import
1212
paths are considered internals and can change in minor and patch releases.
1313

14+
1415
v4.42.1 (unreleased)
1516
--------------------
1617

1718
Fixed
1819
^^^^^
1920
- Prevent extra environment variables in helptext when default_env=True, for
20-
version actions and subcommands(`#787
21+
version actions and subcommands (`#787
2122
<https://github.com/omni-us/jsonargparse/pull/787>`__).
23+
- ``Union`` with path type and non-path value incorrectly dumped as string
24+
(`#789 <https://github.com/omni-us/jsonargparse/pull/789>`__).
25+
2226

2327
v4.42.0 (2025-10-14)
2428
--------------------

jsonargparse/typing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ def _is_path_type(value, type_class):
220220

221221

222222
def _serialize_path(path: Path):
223+
if not isinstance(path, Path):
224+
raise ValueError("Expected a Path instance.")
223225
if path_dump_preserve_relative.get() and path.relative != path.absolute:
224226
return {
225227
"relative": path._relative,

jsonargparse_tests/test_typehints.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,3 +1590,9 @@ def mocked_get_import_path(cls):
15901590
subclass_paths = get_all_subclass_paths(ImportClass)
15911591
assert "Failed to import ImportClass" in str(w[0].message)
15921592
assert subclass_paths == []
1593+
1594+
1595+
def test_non_path_dump(parser):
1596+
parser.add_argument("--data", type=Union[Path_fr, Dict[str, List[str]]])
1597+
cfg = parser.parse_args(['--data={"key": ["value"]}'])
1598+
assert json_or_yaml_load(parser.dump(cfg)) == {"data": {"key": ["value"]}}

0 commit comments

Comments
 (0)