Skip to content

Commit 2bb4a05

Browse files
committed
fix no_args_is_help in click 8.2 and assert behavior in tests
1 parent 57a62c9 commit 2bb4a05

10 files changed

+178
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
## Version 1.8.9 (2025-05-19)
44

5-
- Fix deprecation warning in Click 8.2. [[#215](https://github.com/ewels/rich-click/pull/215)] ([@finsberg](https://github.com/finsberg))
6-
- Fix typing incompatibilities with Click 8.2.
7-
- Added Click 8.2's support for `Parameter.deprecated: str | bool`
5+
Click 8.2 support:
6+
7+
- Fix deprecation warning in Click 8.2. [[#239](https://github.com/ewels/rich-click/pull/239)] ([@finsberg](https://github.com/finsberg))
8+
- Fix typing incompatibilities with Click 8.2. [[#240](https://github.com/ewels/rich-click/pull/240), [[#242](https://github.com/ewels/rich-click/pull/242)] ([@finsberg](https://github.com/finsberg))
9+
- Fixed `no_args_is_help=True` with Click 8.2: [[#241](https://github.com/ewels/rich-click/issues/241)]
10+
- Added Click 8.2's support for `Parameter.deprecated: str | bool` [#242](https://github.com/ewels/rich-click/pull/242)]
811

912
## Version 1.8.8 (2025-03-09)
1013

src/rich_click/patch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class _PatchedRichMultiCommand(RichMultiCommand, _PatchedRichCommand):
2121
pass
2222

2323

24-
class _PatchedRichCommandCollection(RichCommandCollection, _PatchedRichCommand): # type: ignore[misc]
24+
class _PatchedRichCommandCollection(RichCommandCollection, _PatchedRichCommand):
2525
pass
2626

2727

28-
class _PatchedRichGroup(RichGroup, _PatchedRichCommand): # type: ignore[misc]
28+
class _PatchedRichGroup(RichGroup, _PatchedRichCommand):
2929
pass
3030

3131

src/rich_click/rich_command.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
# or else rich_click.cli.patch() causes a recursion error.
1111
from click import CommandCollection, Group
1212
from click.utils import PacifyFlushWrapper, make_str
13+
from typing_extensions import Literal, NoReturn
1314

14-
from rich_click._compat_click import CLICK_IS_BEFORE_VERSION_8X, CLICK_IS_BEFORE_VERSION_9X
15+
from rich_click._compat_click import CLICK_IS_BEFORE_VERSION_8X, CLICK_IS_BEFORE_VERSION_9X, CLICK_IS_BEFORE_VERSION_82
1516
from rich_click.rich_context import RichContext
1617
from rich_click.rich_help_configuration import RichHelpConfiguration
1718
from rich_click.rich_help_formatter import RichHelpFormatter
@@ -112,6 +113,26 @@ def _error_formatter(self, ctx: Optional[RichContext]) -> RichHelpFormatter:
112113
formatter = self.context_class.formatter_class(console=self.console, config=config, file=sys.stderr)
113114
return formatter
114115

116+
@overload
117+
def main(
118+
self,
119+
args: Optional[Sequence[str]] = None,
120+
prog_name: Optional[str] = None,
121+
complete_var: Optional[str] = None,
122+
standalone_mode: Literal[True] = True,
123+
**extra: Any,
124+
) -> NoReturn: ...
125+
126+
@overload
127+
def main(
128+
self,
129+
args: Optional[Sequence[str]] = None,
130+
prog_name: Optional[str] = None,
131+
complete_var: Optional[str] = None,
132+
standalone_mode: bool = ...,
133+
**extra: Any,
134+
) -> Any: ...
135+
115136
def main(
116137
self,
117138
args: Optional[Sequence[str]] = None,
@@ -180,6 +201,11 @@ def main(
180201
except click.exceptions.ClickException as e:
181202
if not standalone_mode:
182203
raise
204+
205+
if not CLICK_IS_BEFORE_VERSION_82:
206+
if isinstance(e, click.exceptions.NoArgsIsHelpError):
207+
sys.exit(e.exit_code)
208+
183209
formatter = self._error_formatter(ctx)
184210
from rich_click.rich_help_rendering import rich_format_error
185211

src/rich_click/rich_help_rendering.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ class MetavarHighlighter(RegexHighlighter):
618618

619619

620620
def get_rich_commands(
621-
obj: MultiCommand, # type: ignore[valid-type]
621+
obj: MultiCommand,
622622
ctx: click.Context,
623623
formatter: RichHelpFormatter,
624624
) -> None:
@@ -629,7 +629,7 @@ def get_rich_commands(
629629
# Look through COMMAND_GROUPS for this command
630630
# stick anything unmatched into a default group at the end
631631
cmd_groups = _resolve_groups(ctx=ctx, groups=formatter.config.command_groups, group_attribute="commands")
632-
for command in obj.list_commands(ctx): # type: ignore[attr-defined]
632+
for command in obj.list_commands(ctx):
633633
for cmd_group in cmd_groups:
634634
if command in cmd_group.get("commands", []):
635635
break
@@ -672,9 +672,9 @@ def get_rich_commands(
672672
)
673673
for command in cmd_group.get("commands", []):
674674
# Skip if command does not exist
675-
if command not in obj.list_commands(ctx): # type: ignore[attr-defined]
675+
if command not in obj.list_commands(ctx):
676676
continue
677-
cmd = obj.get_command(ctx, command) # type: ignore[attr-defined]
677+
cmd = obj.get_command(ctx, command)
678678
if TYPE_CHECKING:
679679
assert cmd is not None
680680
if cmd.hidden:

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class AssertRichFormat(Protocol):
153153
def __call__(
154154
self,
155155
cmd: Union[str, RichCommand, RichGroup],
156-
args: str,
156+
args: Optional[str],
157157
error: Optional[Type[Exception]],
158158
rich_config: Optional[Callable[[Any], Union[RichGroup, RichCommand]]],
159159
) -> None:
@@ -198,7 +198,7 @@ def config_to_dict(config: RichHelpConfiguration) -> Dict[Any, Any]:
198198

199199
def assertion(
200200
cmd: Union[str, RichCommand],
201-
args: str,
201+
args: Optional[str],
202202
error: Optional[Type[Exception]],
203203
rich_config: Optional[Callable[[Any], RichCommand]],
204204
) -> None:
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
Usage: cli [OPTIONS]
3+
4+
My amazing tool does all the things.
5+
6+
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────╮
7+
│ --version Show the version and exit. │
8+
│ --help Show this message and exit. │
9+
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
10+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
{
2+
"style_option": "bold cyan",
3+
"style_argument": "bold cyan",
4+
"style_command": "bold cyan",
5+
"style_switch": "bold green",
6+
"style_metavar": "bold yellow",
7+
"style_metavar_append": "dim yellow",
8+
"style_metavar_separator": "dim",
9+
"style_header_text": "",
10+
"style_epilog_text": "",
11+
"style_footer_text": "",
12+
"style_usage": "yellow",
13+
"style_usage_command": "bold",
14+
"style_deprecated": "red",
15+
"style_helptext_first_line": "",
16+
"style_helptext": "dim",
17+
"style_option_help": "",
18+
"style_option_default": "dim",
19+
"style_option_envvar": "dim yellow",
20+
"style_required_short": "red",
21+
"style_required_long": "dim red",
22+
"style_options_panel_border": "dim",
23+
"style_options_panel_box": "ROUNDED",
24+
"align_options_panel": "left",
25+
"style_options_table_show_lines": false,
26+
"style_options_table_leading": 0,
27+
"style_options_table_pad_edge": false,
28+
"style_options_table_padding": [
29+
0,
30+
1
31+
],
32+
"style_options_table_box": "",
33+
"style_options_table_row_styles": null,
34+
"style_options_table_border_style": null,
35+
"style_commands_panel_border": "dim",
36+
"style_commands_panel_box": "ROUNDED",
37+
"align_commands_panel": "left",
38+
"style_commands_table_show_lines": false,
39+
"style_commands_table_leading": 0,
40+
"style_commands_table_pad_edge": false,
41+
"style_commands_table_padding": [
42+
0,
43+
1
44+
],
45+
"style_commands_table_box": "",
46+
"style_commands_table_row_styles": null,
47+
"style_commands_table_border_style": null,
48+
"style_commands_table_column_width_ratio": [
49+
null,
50+
null
51+
],
52+
"style_errors_panel_border": "red",
53+
"style_errors_panel_box": "ROUNDED",
54+
"align_errors_panel": "left",
55+
"style_errors_suggestion": "dim",
56+
"style_errors_suggestion_command": "blue",
57+
"style_aborted": "red",
58+
"width": 100,
59+
"max_width": null,
60+
"color_system": null,
61+
"force_terminal": true,
62+
"header_text": null,
63+
"footer_text": null,
64+
"deprecated_string": "(Deprecated) ",
65+
"deprecated_with_reason_string": "(Deprecated: {}) ",
66+
"default_string": "[default: {}]",
67+
"envvar_string": "[env var: {}]",
68+
"required_short_string": "*",
69+
"required_long_string": "[required]",
70+
"range_string": " [{}]",
71+
"append_metavars_help_string": "({})",
72+
"arguments_panel_title": "Arguments",
73+
"options_panel_title": "Options",
74+
"commands_panel_title": "Commands",
75+
"errors_panel_title": "Error",
76+
"errors_suggestion": null,
77+
"errors_epilogue": null,
78+
"aborted_text": "Aborted.",
79+
"show_arguments": false,
80+
"show_metavars_column": true,
81+
"append_metavars_help": false,
82+
"group_arguments_options": false,
83+
"option_envvar_first": false,
84+
"text_markup": "ansi",
85+
"use_markdown": false,
86+
"use_markdown_emoji": true,
87+
"use_rich_markup": false,
88+
"command_groups": {},
89+
"option_groups": {},
90+
"use_click_short_help": false,
91+
"highlighter_patterns": [
92+
"(^|[^\\w\\-])(?P<switch>-([^\\W0-9][\\w\\-]*\\w|[^\\W0-9]))",
93+
"(^|[^\\w\\-])(?P<option>--([^\\W0-9][\\w\\-]*\\w|[^\\W0-9]))",
94+
"(?P<metavar><[^>]+>)"
95+
],
96+
"legacy_windows": null
97+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
Usage: cli [OPTIONS]
3+
4+
My amazing tool does all the things.
5+
6+
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────╮
7+
│ --version Show the version and exit. │
8+
│ --help Show this message and exit. │
9+
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯

tests/fixtures/no_args_is_help.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import rich_click as click
2+
3+
4+
@click.command(no_args_is_help=True)
5+
@click.version_option(version="1.2.3")
6+
def cli() -> None:
7+
"""
8+
My amazing tool does all the things.
9+
"""
10+
11+
12+
if __name__ == "__main__":
13+
cli()

tests/test_help.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,19 @@ def default_config(initialize_rich_click: None) -> None:
209209
),
210210
id="test table styles with rich_config",
211211
),
212+
pytest.param(
213+
"no_args_is_help",
214+
None,
215+
None,
216+
rich_config(help_config=RichHelpConfiguration()),
217+
id="no args is help",
218+
),
212219
],
213220
)
214221
@pytest.mark.filterwarnings("ignore:^.*click prior to.*$:RuntimeWarning")
215222
def test_rich_click(
216223
cmd: str,
217-
args: str,
224+
args: Optional[str],
218225
error: Optional[Type[Exception]],
219226
rich_config: Optional[Callable[[Any], Union[RichGroup, RichCommand]]],
220227
assert_rich_format: AssertRichFormat,

0 commit comments

Comments
 (0)