Skip to content

Commit

Permalink
Fix failures when choices is a dict_keys object and value non-hashable.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauvilsa committed Oct 20, 2023
1 parent f58237f commit fd3c57c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ The semantic versioning only considers the public API as described in
paths are considered internals and can change in minor and patch releases.


v4.26.1 (2023-10-??)
--------------------

Fixed
^^^^^
- Failures when choices is a ``dict_keys`` object and value non-hashable.


v4.26.0 (2023-10-19)
--------------------

Expand Down
2 changes: 2 additions & 0 deletions jsonargparse/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ def add_argument(self, *args, enable_path: bool = False, **kwargs):
container=super(),
logger=self._logger,
)
if "choices" in kwargs and not isinstance(kwargs["choices"], (list, tuple)):
kwargs["choices"] = tuple(kwargs["choices"])
action = super().add_argument(*args, **kwargs)
action.logger = self._logger # type: ignore
ActionConfigFile._add_print_config_argument(self, action)
Expand Down
9 changes: 9 additions & 0 deletions jsonargparse_tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ def test_parse_args_choices_config(parser):
pytest.raises(ArgumentError, lambda: parser.parse_args(["--cfg=ch2: v0"]))


def test_parse_args_non_hashable_choice(parser):
choices = {"A": 1, "B": 2}
parser.add_argument("--cfg", action=ActionConfigFile)
parser.add_argument("--ch1", choices=choices.keys())
with pytest.raises(ArgumentError) as ctx:
parser.parse_args(["--cfg=ch1: [1,2]"])
ctx.match("not among choices")


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

0 comments on commit fd3c57c

Please sign in to comment.