Skip to content

Commit

Permalink
The CLASS_PATH_OR_NAME for subclass help is now optional and if not g…
Browse files Browse the repository at this point in the history
…iven the help of the base class is printed (#624).
  • Loading branch information
mauvilsa committed Nov 20, 2024
1 parent 3f4a80b commit 75e1d78
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ The semantic versioning only considers the public API as described in
paths are considered internals and can change in minor and patch releases.


v4.35.0 (2024-12-??)
--------------------

Changed
^^^^^^^
- The ``CLASS_PATH_OR_NAME`` for subclass help is now optional and if not given
the help of the base class is printed (`#???
<https://github.com/omni-us/jsonargparse/pull/???>`__).


v4.34.1 (2024-11-??)
--------------------

Expand Down
12 changes: 10 additions & 2 deletions jsonargparse/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
change_to_path_dir,
default_config_option_help,
get_import_path,
get_typehint_origin,
import_object,
indent_text,
iter_to_set_str,
Expand Down Expand Up @@ -350,8 +351,12 @@ def __init__(self, typehint=None, **kwargs):
super().__init__(**kwargs)

def update_init_kwargs(self, kwargs):
from ._typehints import get_subclass_names
from ._typehints import get_optional_arg, get_subclass_names, get_unaliased_type

typehint = get_unaliased_type(get_optional_arg(self._typehint))
if get_typehint_origin(typehint) is not Union:
assert "nargs" not in kwargs
kwargs["nargs"] = "?"
self._basename = iter_to_set_str(get_subclass_names(self._typehint, callable_return=True))
kwargs.update(
{
Expand Down Expand Up @@ -380,7 +385,10 @@ def print_help(self, call_args):
try:
typehint = get_unaliased_type(get_optional_arg(self._typehint))
baseclasses = get_subclass_types(typehint, callable_return=True)
val_class = import_object(resolve_class_path_by_name(typehint, value))
if self.nargs == "?" and value is None:
val_class = typehint
else:
val_class = import_object(resolve_class_path_by_name(typehint, value))
except Exception as ex:
raise TypeError(f"{option_string}: {ex}") from ex
if not any(is_subclass(val_class, b) for b in baseclasses):
Expand Down
5 changes: 4 additions & 1 deletion jsonargparse_tests/test_subclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,9 @@ def test_subclass_nested_help(parser):
help_str = get_parse_args_stdout(parser, [f"--op.help={__name__}.Nested", "--op.cal.help=TextCalendar"])
assert "Help for --op.cal.help=calendar.TextCalendar" in help_str
assert "--op.cal.firstweekday" in help_str
help_str = get_parse_args_stdout(parser, [f"--op.help={__name__}.Nested", "--op.cal.help"])
assert "Help for --op.cal.help=calendar.Calendar" in help_str
assert "--op.cal.firstweekday" in help_str

with pytest.raises(ArgumentError) as ctx:
parser.parse_args([f"--op.help={__name__}.Nested", "--op.p1=1"])
Expand Down Expand Up @@ -1346,7 +1349,7 @@ def test_add_subclass_required_group(parser):
parser.add_subclass_arguments(Calendar, "cal", required=True)
pytest.raises(ArgumentError, lambda: parser.parse_args([]))
help_str = get_parser_help(parser)
assert "[-h] [--cal.help CLASS_PATH_OR_NAME] --cal " in help_str
assert "[-h] [--cal.help [CLASS_PATH_OR_NAME]] --cal " in help_str


def test_add_subclass_not_required_group(parser):
Expand Down

0 comments on commit 75e1d78

Please sign in to comment.