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
8 changes: 5 additions & 3 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ Added
^^^^^
- Support for Python 3.13 (`#554
<https://github.com/omni-us/jsonargparse/pull/554>`__).
- Support for `NotRequired` and `Required` annotations for `TypedDict` keys
(`#571 <https://github.com/omni-us/jsonargparse/pull/571>`__).
- Support for ``NotRequired`` and ``Required`` annotations for ``TypedDict``
keys (`#571 <https://github.com/omni-us/jsonargparse/pull/571>`__).

Fixed
^^^^^
- Callable type with subclass return not showing the ``--*.help`` option (`#567
<https://github.com/omni-us/jsonargparse/pull/567>`__).
- Forward referenced types not compatible with `Type` typehint (`#576
- Forward referenced types not compatible with ``Type`` typehint (`#576
<https://github.com/omni-us/jsonargparse/pull/576/>`__).
- Subclass nested in ``Iterable`` makes help fail (`#578
<https://github.com/omni-us/jsonargparse/pull/578>`__).
- ``Literal`` mixing enum values and strings failing to parse (`#580
<https://github.com/omni-us/jsonargparse/pull/580/>`__).

Changed
^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ 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})]
subtypes = Union[tuple({type(v) for v in subtypehints if type(v) is not str})]
val = adapt_typehints(val, subtypes, **adapt_kwargs)
if val not in subtypehints:
raise_unexpected_value(f"Expected a {typehint}", val)
Expand Down
10 changes: 10 additions & 0 deletions jsonargparse_tests/test_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ def test_enum_str_optional(parser):
assert None is parser.parse_args(["--enum=null"]).enum


@pytest.mark.skipif(not Literal, reason="Literal introduced in python 3.8 or backported in typing_extensions")
def test_literal_enum_values(parser):
parser.add_argument("--enum", type=Literal[EnumABC.A, EnumABC.C, "X"])
assert EnumABC.A == parser.parse_args(["--enum=A"]).enum
assert EnumABC.C == parser.parse_args(["--enum=C"]).enum
assert "X" == parser.parse_args(["--enum=X"]).enum
with pytest.raises(ArgumentError, match="Expected a typing.*.Literal"):
parser.parse_args(["--enum=B"])


# set tests


Expand Down