Skip to content

Commit 2bcbd48

Browse files
authored
Now --*.help output shows options without init_args (#533)
1 parent 6c89858 commit 2bcbd48

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

CHANGELOG.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ The semantic versioning only considers the public API as described in
1212
paths are considered internals and can change in minor and patch releases.
1313

1414

15+
v4.31.0 (2024-06-??)
16+
--------------------
17+
18+
Changed
19+
^^^^^^^
20+
- Now ``--*.help`` output shows options without ``init_args`` (`#533
21+
<https://github.com/omni-us/jsonargparse/pull/533>`__).
22+
23+
1524
v4.30.0 (2024-06-18)
1625
--------------------
1726

jsonargparse/_actions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
argument_error,
2121
change_to_path_dir,
2222
default_config_option_help,
23+
get_import_path,
2324
get_typehint_origin,
2425
import_object,
2526
indent_text,
@@ -387,8 +388,7 @@ def print_help(self, call_args, baseclass, dest):
387388
baseclasses = [baseclass]
388389
if not any(is_subclass(val_class, b) for b in baseclasses):
389390
raise TypeError(f'{option_string}: Class "{value}" is not a subclass of {self._basename}')
390-
dest += ".init_args"
391-
subparser = type(parser)()
391+
subparser = type(parser)(description=f"Help for {option_string}={get_import_path(val_class)}")
392392
subparser.add_class_arguments(val_class, dest, **self.sub_add_kwargs)
393393
remove_actions(subparser, (_HelpAction, _ActionPrintConfig, _ActionConfigLoad))
394394
args = self.get_args_after_opt(parser.args)

jsonargparse_tests/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
set_docstring_parse_options(style=DocstringStyle.GOOGLE)
2828

2929

30+
columns_env = {"COLUMNS": "200"}
31+
3032
is_cpython = platform.python_implementation() == "CPython"
3133
is_posix = os.name == "posix"
3234

@@ -153,7 +155,7 @@ def source_unavailable():
153155

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

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

jsonargparse_tests/test_subclasses.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def test_subclass_union_help(parser):
156156
help_str = get_parser_help(parser)
157157
assert "Show the help for the given subclass of Calendar" in help_str
158158
help_str = get_parse_args_stdout(parser, ["--op.help", "TextCalendar"])
159-
assert "--op.init_args.firstweekday" in help_str
159+
assert "--op.firstweekday" in help_str
160160

161161

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

210210

211211
class MergeInitArgs(Calendar):
@@ -529,11 +529,12 @@ def test_subclass_nested_parse(parser, prefix):
529529

530530
def test_subclass_nested_help(parser):
531531
parser.add_argument("--op", type=Nested)
532-
help_str = get_parse_args_stdout(parser, [f"--op.help={__name__}.Nested", "--op.init_args.cal.help=TextCalendar"])
533-
assert "--op.init_args.cal.init_args.firstweekday" in help_str
532+
help_str = get_parse_args_stdout(parser, [f"--op.help={__name__}.Nested", "--op.cal.help=TextCalendar"])
533+
assert "Help for --op.cal.help=calendar.TextCalendar" in help_str
534+
assert "--op.cal.firstweekday" in help_str
534535

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

539540

@@ -580,7 +581,8 @@ def test_subclass_class_name_parse(parser):
580581
def test_subclass_class_name_help(parser):
581582
parser.add_argument("--op", type=Union[Calendar, GzipFile, None])
582583
help_str = get_parse_args_stdout(parser, ["--op.help=GzipFile"])
583-
assert "--op.init_args.compresslevel" in help_str
584+
assert "Help for --op.help=gzip.GzipFile" in help_str
585+
assert "--op.compresslevel" in help_str
584586

585587

586588
class LocaleTextCalendar(Calendar):
@@ -1318,7 +1320,7 @@ def test_add_subclass_tuple(parser):
13181320
assert isinstance(init.c, TupleBaseB)
13191321

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

13231325

13241326
def test_add_subclass_required_group(parser):

jsonargparse_tests/test_typehints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ def test_union_new_syntax_simple_types(parser):
590590
def test_union_new_syntax_subclass_type(parser):
591591
parser.add_argument("--op", type=eval("Calendar | bool"))
592592
help_str = get_parse_args_stdout(parser, ["--op.help=calendar.TextCalendar"])
593-
assert "--op.init_args.firstweekday" in help_str
593+
assert "--op.firstweekday" in help_str
594594

595595

596596
# callable tests

0 commit comments

Comments
 (0)