1
+ # mypy: ignore-errors
1
2
"""Tests for add_help parameter functionality."""
2
3
3
4
import dataclasses
@@ -22,15 +23,15 @@ def simple_function(x: int, y: str = "hello") -> str:
22
23
return f"{ x } :{ y } "
23
24
24
25
25
- def test_cli_add_help_false ():
26
+ def test_cli_add_help_false () -> None :
26
27
"""Test that add_help=False prevents help from being added."""
27
28
# Should raise an error when --help is provided with add_help=False
28
29
with pytest .raises (SystemExit ) as excinfo :
29
30
tyro .cli (SimpleConfig , args = ["--help" ], add_help = False )
30
31
assert excinfo .value .code == 2 # Should fail with parsing error, not help display
31
32
32
33
33
- def test_cli_add_help_true ():
34
+ def test_cli_add_help_true () -> None :
34
35
"""Test that add_help=True (default) allows help."""
35
36
# Should show help and exit with code 0
36
37
with pytest .raises (SystemExit ) as excinfo :
@@ -44,7 +45,7 @@ def test_cli_add_help_true():
44
45
assert excinfo .value .code == 0 # Should exit cleanly after showing help
45
46
46
47
47
- def test_cli_default_add_help ():
48
+ def test_cli_default_add_help () -> None :
48
49
"""Test that add_help defaults to True."""
49
50
# Should show help and exit with code 0
50
51
with pytest .raises (SystemExit ) as excinfo :
@@ -57,14 +58,14 @@ def test_cli_default_add_help():
57
58
assert excinfo .value .code == 0
58
59
59
60
60
- def test_get_parser_add_help_false ():
61
+ def test_get_parser_add_help_false () -> None :
61
62
"""Test that get_parser with add_help=False doesn't add help option."""
62
63
parser = tyro .extras .get_parser (SimpleConfig , add_help = False )
63
64
assert "-h" not in parser ._option_string_actions
64
65
assert "--help" not in parser ._option_string_actions
65
66
66
67
67
- def test_get_parser_add_help_true ():
68
+ def test_get_parser_add_help_true () -> None :
68
69
"""Test that get_parser with add_help=True adds help option."""
69
70
parser = tyro .extras .get_parser (SimpleConfig , add_help = True )
70
71
assert (
@@ -73,7 +74,7 @@ def test_get_parser_add_help_true():
73
74
)
74
75
75
76
76
- def test_get_parser_default_add_help ():
77
+ def test_get_parser_default_add_help () -> None :
77
78
"""Test that get_parser defaults to add_help=True."""
78
79
parser = tyro .extras .get_parser (SimpleConfig )
79
80
assert (
@@ -82,7 +83,7 @@ def test_get_parser_default_add_help():
82
83
)
83
84
84
85
85
- def test_function_cli_add_help ():
86
+ def test_function_cli_add_help () -> None :
86
87
"""Test add_help works with function targets."""
87
88
# Test with add_help=False
88
89
result = tyro .cli (simple_function , args = ["--x" , "42" ], add_help = False )
@@ -94,7 +95,7 @@ def test_function_cli_add_help():
94
95
assert excinfo .value .code == 2
95
96
96
97
97
- def test_subcommand_app_add_help ():
98
+ def test_subcommand_app_add_help () -> None :
98
99
"""Test add_help parameter with SubcommandApp."""
99
100
from tyro .extras import SubcommandApp
100
101
@@ -118,7 +119,7 @@ def cmd2(y: str) -> str:
118
119
assert excinfo .value .code == 2
119
120
120
121
121
- def test_subcommand_cli_from_dict_add_help ():
122
+ def test_subcommand_cli_from_dict_add_help () -> None :
122
123
"""Test add_help parameter with subcommand_cli_from_dict."""
123
124
124
125
def cmd1 (x : int ) -> int :
@@ -143,7 +144,7 @@ def cmd2(y: str) -> str:
143
144
assert excinfo .value .code == 2
144
145
145
146
146
- def test_overridable_config_cli_add_help ():
147
+ def test_overridable_config_cli_add_help () -> None :
147
148
"""Test add_help parameter with overridable_config_cli."""
148
149
149
150
@dataclasses .dataclass
@@ -167,7 +168,7 @@ class Config:
167
168
assert excinfo .value .code == 2
168
169
169
170
170
- def test_subparsers_respect_add_help ():
171
+ def test_subparsers_respect_add_help () -> None :
171
172
"""Test that subparsers inherit the add_help setting from parent parser."""
172
173
173
174
@dataclasses .dataclass
@@ -199,7 +200,7 @@ class ConfigB:
199
200
assert result .value_a == 1
200
201
201
202
202
- def test_return_unknown_args_with_add_help_false ():
203
+ def test_return_unknown_args_with_add_help_false () -> None :
203
204
"""Test that --help/-h are returned as unknown args when add_help=False and return_unknown_args=True."""
204
205
205
206
@dataclasses .dataclass
@@ -241,3 +242,36 @@ class Config:
241
242
finally :
242
243
sys .stdout = old_stdout
243
244
assert excinfo .value .code == 0 # Should exit cleanly after showing help
245
+
246
+
247
+ def test_error_messages_respect_add_help () -> None :
248
+ """Test that error messages don't suggest --help when add_help=False."""
249
+ import contextlib
250
+
251
+ @dataclasses .dataclass
252
+ class Config :
253
+ required_field : int
254
+
255
+ # Capture stderr to check error messages
256
+ captured_output = io .StringIO ()
257
+
258
+ # Test with add_help=False - should not mention --help
259
+ with pytest .raises (SystemExit ):
260
+ with contextlib .redirect_stderr (captured_output ):
261
+ tyro .cli (Config , args = [], add_help = False , console_outputs = True )
262
+
263
+ error_message = captured_output .getvalue ()
264
+ assert "--help" not in error_message , (
265
+ f"Error message should not mention --help when add_help=False. Got: { error_message } "
266
+ )
267
+
268
+ # Test with add_help=True - should mention --help
269
+ captured_output = io .StringIO ()
270
+ with pytest .raises (SystemExit ):
271
+ with contextlib .redirect_stderr (captured_output ):
272
+ tyro .cli (Config , args = [], add_help = True , console_outputs = True )
273
+
274
+ error_message = captured_output .getvalue ()
275
+ assert "--help" in error_message , (
276
+ f"Error message should mention --help when add_help=True. Got: { error_message } "
277
+ )
0 commit comments