From c4fe9b0bbb6ae6b58cfef54bf18373b0049ad4a2 Mon Sep 17 00:00:00 2001 From: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> Date: Wed, 18 Sep 2024 06:33:09 +0200 Subject: [PATCH 1/2] Fix subclass nested in Iterable makes help fail (lightning#20199). --- CHANGELOG.rst | 2 ++ jsonargparse/_typehints.py | 2 +- jsonargparse_tests/test_subclasses.py | 6 ++++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a9361da2..0ac4be51 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -29,6 +29,8 @@ Fixed - Forward referenced types not compatible with `Type` typehint (`#576 `__) +- Subclass nested in ``Iterable`` makes help fail (`#??? + `__). Changed ^^^^^^^ diff --git a/jsonargparse/_typehints.py b/jsonargparse/_typehints.py index b1a20bcf..50a119b8 100644 --- a/jsonargparse/_typehints.py +++ b/jsonargparse/_typehints.py @@ -1226,7 +1226,7 @@ def is_private(class_path): return "._" in class_path def add_subclasses(cl): - if hasattr(cl, "__args__") and get_typehint_origin(cl) in {List, list, Union}: + if hasattr(cl, "__args__") and get_typehint_origin(cl) in sequence_origin_types.union({Union}): for arg in cl.__args__: add_subclasses(arg) return diff --git a/jsonargparse_tests/test_subclasses.py b/jsonargparse_tests/test_subclasses.py index 3d5d8149..a5a4d379 100644 --- a/jsonargparse_tests/test_subclasses.py +++ b/jsonargparse_tests/test_subclasses.py @@ -742,12 +742,14 @@ def __init__(self, p1: int = 0, p2: int = 0): pass -def test_subclass_list_append_single(parser): - parser.add_argument("--val", type=Union[ListAppend, List[ListAppend]]) +@pytest.mark.parametrize("list_type", [List, Iterable]) +def test_subclass_list_append_single(parser, list_type): + parser.add_argument("--val", type=Union[ListAppend, list_type[ListAppend]]) cfg = parser.parse_args([f"--val+={__name__}.ListAppend", "--val.p1=1", "--val.p2=2", "--val.p1=3"]) assert cfg.val == [Namespace(class_path=f"{__name__}.ListAppend", init_args=Namespace(p1=3, p2=2))] cfg = parser.parse_args(["--val+=ListAppend", "--val.p2=2", "--val.p1=1"]) assert cfg.val == [Namespace(class_path=f"{__name__}.ListAppend", init_args=Namespace(p1=1, p2=2))] + assert " --val+ " in get_parser_help(parser) @final From 6d0c3e0eade3d20f49ae6c68a0fc747fb95b03fa Mon Sep 17 00:00:00 2001 From: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> Date: Wed, 18 Sep 2024 06:40:32 +0200 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0ac4be51..36ebe68e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -26,11 +26,10 @@ Fixed ^^^^^ - Callable type with subclass return not showing the ``--*.help`` option (`#567 `__). - - Forward referenced types not compatible with `Type` typehint (`#576 - `__) -- Subclass nested in ``Iterable`` makes help fail (`#??? - `__). + `__). +- Subclass nested in ``Iterable`` makes help fail (`#578 + `__). Changed ^^^^^^^