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 @@ -22,6 +22,8 @@ Fixed
<https://github.com/omni-us/jsonargparse/pull/787>`__).
- ``Union`` with path type and non-path value incorrectly dumped as string
(`#789 <https://github.com/omni-us/jsonargparse/pull/789>`__).
- Misleading error message for invalid value for ``Literal`` strings (`#790
<https://github.com/omni-us/jsonargparse/pull/790>`__).


v4.42.0 (2025-10-14)
Expand Down
5 changes: 3 additions & 2 deletions jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,8 +781,9 @@ def adapt_typehints(
# Literal
elif typehint_origin in literal_types:
if val not in subtypehints and isinstance(val, str):
subtypes = Union[tuple((type(v) for v in subtypehints if type(v) is not str))]
val = adapt_typehints(val, subtypes, **adapt_kwargs)
subtypes = tuple((type(v) for v in subtypehints if type(v) is not str))
if subtypes:
val = adapt_typehints(val, Union[subtypes], **adapt_kwargs)
if val not in subtypehints:
raise_unexpected_value(f"Expected a {typehint}", val)

Expand Down
2 changes: 2 additions & 0 deletions jsonargparse_tests/test_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ def test_union_of_literals(parser):
parser.add_argument("--literal", type=Union[Literal[1, 2], Literal["a", "b"]])
assert "a" == parser.parse_args(["--literal=a"]).literal
assert 2 == parser.parse_args(["--literal=2"]).literal
with pytest.raises(ArgumentError, match=r"Expected a typing.Literal\['a', 'b']"):
parser.parse_args(["--literal=x"])


@parser_modes
Expand Down