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
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ The semantic versioning only considers the public API as described in
paths are considered internals and can change in minor and patch releases.


v4.45.1 (unreleased)
--------------------

Fixed
^^^^^
- Choices with an int type not working correctly (`#827
<https://github.com/omni-us/jsonargparse/pull/827>`__).


v4.45.0 (2025-12-26)
--------------------

Expand Down
2 changes: 2 additions & 0 deletions jsonargparse/_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ def prepare_add_argument(args, kwargs, enable_path, container, logger, sub_add_k
if sub_add_kwargs:
help_action.sub_add_kwargs = sub_add_kwargs
kwargs["action"] = ActionTypeHint(typehint=typehint, enable_path=enable_path, logger=logger)
if kwargs.get("choices"):
kwargs["type"] = lambda v: adapt_typehints(v, typehint)
return args

@staticmethod
Expand Down
12 changes: 12 additions & 0 deletions jsonargparse_tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from io import StringIO
from pathlib import Path
from random import randint, shuffle
from typing import Union
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -183,6 +184,17 @@ def test_parse_args_choices_nargs_plus(parser):
ctx.match("invalid choice")


def test_parse_args_choices_type_int(parser):
parser.add_argument("--ch", type=int, choices=[0, 1])
assert 0 == parser.parse_args(["--ch=0"]).ch


def test_parse_args_choices_type_union(parser):
parser.add_argument("--ch", type=Union[int, str], choices=[0, 1, "x", "y"])
assert 0 == parser.parse_args(["--ch=0"]).ch
assert "x" == parser.parse_args(["--ch=x"]).ch


def test_parse_object_simple(parser):
parser.add_argument("--op", type=int)
assert parser.parse_object({"op": 1}) == Namespace(op=1)
Expand Down
Loading