Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The CLASS_PATH_OR_NAME for subclass help is now optional and if not given the help of the base class is printed #628

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
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 (`#628
<https://github.com/omni-us/jsonargparse/pull/628>`__).


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
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
Loading