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 (#628)
  • Loading branch information
mauvilsa authored Dec 2, 2024
1 parent e9eca4c commit a2b4e3d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Changed
- Argument groups created from dataclass-like that have zero configurable
arguments no longer adds a config loader (`#634
<https://github.com/omni-us/jsonargparse/pull/634>`__).
- The ``CLASS_PATH_OR_NAME`` for subclass help is now optional and if not given
the help of the base class is printed (`#628
<https://github.com/omni-us/jsonargparse/pull/628>`__).

Deprecated
^^^^^^^^^^
Expand All @@ -29,6 +32,7 @@ Deprecated


v4.34.1 (2024-12-02)
--------------------

Fixed
^^^^^
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 @@ -346,8 +347,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 @@ -376,7 +381,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
2 changes: 1 addition & 1 deletion jsonargparse/_link_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def __init__(
help_str: Optional[str]
if is_target_subclass and not valid_target_leaf:
type_attr = None
help_str = f"Use --{self.target[1].dest}.help CLASS_PATH for details."
help_str = f"Use --{self.target[1].dest}.help for details."
else:
type_attr = getattr(self.target[1], "_typehint", self.target[1].type)
help_str = self.target[1].help
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 a2b4e3d

Please sign in to comment.