Skip to content

Commit 3d6ebc7

Browse files
e3rdbrentyi
andauthored
Improve prefix omitting flag keep groups in help (#252)
* Improve prefix omitting flag keep groups in help * ruff --------- Co-authored-by: brentyi <yibrenth@gmail.com>
1 parent f144f2b commit 3d6ebc7

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

src/tyro/_parsers.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,6 @@ def apply_args(
247247
def format_group_name(group_name: str) -> str:
248248
return (group_name + " options").strip()
249249

250-
def group_name_from_arg(arg: _arguments.ArgumentDefinition) -> str:
251-
prefix = arg.lowered.name_or_flags[0]
252-
if prefix.startswith("--"):
253-
prefix = prefix[2:]
254-
if "." in prefix:
255-
prefix = prefix.rpartition(".")[0]
256-
else:
257-
prefix = ""
258-
return prefix
259-
260250
group_from_group_name: Dict[str, argparse._ArgumentGroup] = {
261251
"": parser._action_groups[1],
262252
**{
@@ -274,7 +264,7 @@ def group_name_from_arg(arg: _arguments.ArgumentDefinition) -> str:
274264
if arg.is_suppressed():
275265
continue
276266

277-
group_name = group_name_from_arg(arg)
267+
group_name = arg.extern_prefix
278268
if group_name not in group_from_group_name:
279269
description = (
280270
parent.helptext_from_intern_prefixed_field_name.get(

tests/test_conf.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -1112,9 +1112,15 @@ class TrainConfig:
11121112
args="--model.num-slots 3".split(" "),
11131113
) == TrainConfig(ModelConfig(num_slots=3))
11141114

1115-
assert tyro.cli(
1116-
tyro.conf.OmitArgPrefixes[TrainConfig], args="--num-slots 3".split(" ")
1117-
) == TrainConfig(ModelConfig(num_slots=3))
1115+
annot = tyro.conf.OmitArgPrefixes[TrainConfig]
1116+
assert tyro.cli(annot, args="--num-slots 3".split(" ")) == TrainConfig(
1117+
ModelConfig(num_slots=3)
1118+
)
1119+
1120+
# groups are still printed in the help text
1121+
help_text = get_helptext_with_checks(annot)
1122+
assert "model options" in help_text
1123+
assert "--num-slots" in help_text
11181124

11191125

11201126
def test_custom_constructor_0() -> None:
@@ -1539,8 +1545,8 @@ class DatasetConfig:
15391545
with pytest.raises(SystemExit), contextlib.redirect_stdout(target):
15401546
instantiate_dataclasses((OptimizerConfig, DatasetConfig), args=["--help"])
15411547
helptext = target.getvalue()
1542-
assert "OptimizerConfig options" not in helptext
1543-
assert "DatasetConfig options" not in helptext
1548+
assert "OptimizerConfig options" in helptext
1549+
assert "DatasetConfig options" in helptext
15441550

15451551

15461552
def test_counter_action() -> None:

tests/test_py311_generated/test_conf_generated.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1546,8 +1546,8 @@ class DatasetConfig:
15461546
with pytest.raises(SystemExit), contextlib.redirect_stdout(target):
15471547
instantiate_dataclasses((OptimizerConfig, DatasetConfig), args=["--help"])
15481548
helptext = target.getvalue()
1549-
assert "OptimizerConfig options" not in helptext
1550-
assert "DatasetConfig options" not in helptext
1549+
assert "OptimizerConfig options" in helptext
1550+
assert "DatasetConfig options" in helptext
15511551

15521552

15531553
def test_counter_action() -> None:

0 commit comments

Comments
 (0)