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
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ paths are considered internals and can change in minor and patch releases.
v4.44.0 (unreleased)
--------------------

Fixed
^^^^^
- Union types with str and default comment-like string incorrectly parsed as a
stringified exception of an other subtype (`#812
<https://github.com/omni-us/jsonargparse/pull/812>`__).

Changed
^^^^^^^
- Improved error messages when not accepted options are given, referencing which
Expand Down
2 changes: 1 addition & 1 deletion jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ def adapt_typehints(
vals.append(ex)
if all(isinstance(v, Exception) for v in vals):
raise_union_unexpected_value(sorted_subtypes, val, vals)
val = vals[-1]
val = next((v for v in reversed(vals) if not isinstance(v, Exception)))

# Tuple or Set
elif typehint_origin in tuple_set_origin_types:
Expand Down
6 changes: 6 additions & 0 deletions jsonargparse_tests/test_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ def test_str_edge_cases(parser):
assert parser.parse_args([f"--val={val}"]).val == val


def test_str_union_default_comment_like(parser):
parser.add_argument("--val", type=Union[str, int], default="#default")
assert "#default" == parser.get_defaults().val
assert "#default" == parser.parse_args([]).val


def test_bool_parse(parser):
parser.add_argument("--val", type=bool)
assert None is parser.get_defaults().val
Expand Down
Loading