Skip to content

Commit

Permalink
Now --*.help output shows options without init_args (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
mauvilsa authored Jun 20, 2024
1 parent 6c89858 commit 2bcbd48
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ The semantic versioning only considers the public API as described in
paths are considered internals and can change in minor and patch releases.


v4.31.0 (2024-06-??)
--------------------

Changed
^^^^^^^
- Now ``--*.help`` output shows options without ``init_args`` (`#533
<https://github.com/omni-us/jsonargparse/pull/533>`__).


v4.30.0 (2024-06-18)
--------------------

Expand Down
4 changes: 2 additions & 2 deletions jsonargparse/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
argument_error,
change_to_path_dir,
default_config_option_help,
get_import_path,
get_typehint_origin,
import_object,
indent_text,
Expand Down Expand Up @@ -387,8 +388,7 @@ def print_help(self, call_args, baseclass, dest):
baseclasses = [baseclass]
if not any(is_subclass(val_class, b) for b in baseclasses):
raise TypeError(f'{option_string}: Class "{value}" is not a subclass of {self._basename}')
dest += ".init_args"
subparser = type(parser)()
subparser = type(parser)(description=f"Help for {option_string}={get_import_path(val_class)}")
subparser.add_class_arguments(val_class, dest, **self.sub_add_kwargs)
remove_actions(subparser, (_HelpAction, _ActionPrintConfig, _ActionConfigLoad))
args = self.get_args_after_opt(parser.args)
Expand Down
6 changes: 4 additions & 2 deletions jsonargparse_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
set_docstring_parse_options(style=DocstringStyle.GOOGLE)


columns_env = {"COLUMNS": "200"}

is_cpython = platform.python_implementation() == "CPython"
is_posix = os.name == "posix"

Expand Down Expand Up @@ -153,7 +155,7 @@ def source_unavailable():

def get_parser_help(parser: ArgumentParser, strip=False) -> str:
out = StringIO()
with patch.dict(os.environ, {"COLUMNS": "200"}):
with patch.dict(os.environ, columns_env):
parser.print_help(out)
if strip:
return re.sub(" *", " ", out.getvalue())
Expand All @@ -162,7 +164,7 @@ def get_parser_help(parser: ArgumentParser, strip=False) -> str:

def get_parse_args_stdout(parser: ArgumentParser, args: List[str]) -> str:
out = StringIO()
with redirect_stdout(out), pytest.raises(SystemExit):
with patch.dict(os.environ, columns_env), redirect_stdout(out), pytest.raises(SystemExit):
parser.parse_args(args)
return out.getvalue()

Expand Down
18 changes: 10 additions & 8 deletions jsonargparse_tests/test_subclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def test_subclass_union_help(parser):
help_str = get_parser_help(parser)
assert "Show the help for the given subclass of Calendar" in help_str
help_str = get_parse_args_stdout(parser, ["--op.help", "TextCalendar"])
assert "--op.init_args.firstweekday" in help_str
assert "--op.firstweekday" in help_str


class DefaultsDisabled:
Expand Down Expand Up @@ -204,8 +204,8 @@ def func_subclass_untyped(c1: Union[int, UntypedParams]):
def test_subclass_allow_untyped_parameters_help(parser):
parser.add_function_arguments(func_subclass_untyped, fail_untyped=False)
help_str = get_parse_args_stdout(parser, [f"--c1.help={__name__}.UntypedParams"])
assert "--c1.init_args.a1 A1" in help_str
assert "--c1.init_args.a2 A2" in help_str
assert "--c1.a1 A1" in help_str
assert "--c1.a2 A2" in help_str


class MergeInitArgs(Calendar):
Expand Down Expand Up @@ -529,11 +529,12 @@ def test_subclass_nested_parse(parser, prefix):

def test_subclass_nested_help(parser):
parser.add_argument("--op", type=Nested)
help_str = get_parse_args_stdout(parser, [f"--op.help={__name__}.Nested", "--op.init_args.cal.help=TextCalendar"])
assert "--op.init_args.cal.init_args.firstweekday" in help_str
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

with pytest.raises(ArgumentError) as ctx:
parser.parse_args([f"--op.help={__name__}.Nested", "--op.init_args.p1=1"])
parser.parse_args([f"--op.help={__name__}.Nested", "--op.p1=1"])
ctx.match("Expected a nested --\\*.help option")


Expand Down Expand Up @@ -580,7 +581,8 @@ def test_subclass_class_name_parse(parser):
def test_subclass_class_name_help(parser):
parser.add_argument("--op", type=Union[Calendar, GzipFile, None])
help_str = get_parse_args_stdout(parser, ["--op.help=GzipFile"])
assert "--op.init_args.compresslevel" in help_str
assert "Help for --op.help=gzip.GzipFile" in help_str
assert "--op.compresslevel" in help_str


class LocaleTextCalendar(Calendar):
Expand Down Expand Up @@ -1318,7 +1320,7 @@ def test_add_subclass_tuple(parser):
assert isinstance(init.c, TupleBaseB)

help_str = get_parse_args_stdout(parser, [f"--c.help={__name__}.TupleBaseB"])
assert "--c.init_args.b1" in help_str
assert "--c.b1 B1" in help_str


def test_add_subclass_required_group(parser):
Expand Down
2 changes: 1 addition & 1 deletion jsonargparse_tests/test_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def test_union_new_syntax_simple_types(parser):
def test_union_new_syntax_subclass_type(parser):
parser.add_argument("--op", type=eval("Calendar | bool"))
help_str = get_parse_args_stdout(parser, ["--op.help=calendar.TextCalendar"])
assert "--op.init_args.firstweekday" in help_str
assert "--op.firstweekday" in help_str


# callable tests
Expand Down

0 comments on commit 2bcbd48

Please sign in to comment.