Skip to content

Commit 2587813

Browse files
committed
Fixed crash in Python 3.15.0a6
The crash was caused by changes in the argparse internal API in Python 3.15, specifically in how it handles colorization and formatter initialization. Changes Made: 1. Cmd2HelpFormatter._set_color: Added an override for the _set_color method to handle the new file keyword argument introduced in Python 3.15. It uses a try-except block to fall back to the older signature if the underlying RichHelpFormatter (from rich-argparse) does not yet support the new keyword argument. 2. Cmd2ArgumentParser._get_formatter: Updated the _get_formatter method to accept **kwargs and pass them to the superclass. This is necessary because Python 3.15's argparse now passes a file argument to this method in several places (e.g., print_usage). 3. TextGroup.__init__: Updated the type hint for the formatter_creator parameter from Callable[[], Cmd2HelpFormatter] to Callable[..., Cmd2HelpFormatter] to remain consistent with the updated _get_formatter signature.
1 parent 42d2271 commit 2587813

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

cmd2/argparse_custom.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,18 @@ def console(self, console: Cmd2RichArgparseConsole) -> None:
10301030
"""Set our console instance."""
10311031
self._console = console
10321032

1033+
def _set_color(self, color: bool, **kwargs: Any) -> None:
1034+
"""Set the color for the help output.
1035+
1036+
This override is needed because Python 3.15 added a 'file' keyword argument
1037+
to _set_color() which some versions of RichHelpFormatter don't support.
1038+
"""
1039+
try:
1040+
super()._set_color(color, **kwargs)
1041+
except TypeError:
1042+
# Fallback for older versions of RichHelpFormatter that don't support keyword arguments
1043+
super()._set_color(color)
1044+
10331045
def _build_nargs_range_str(self, nargs_range: tuple[int, int | float]) -> str:
10341046
"""Generate nargs range string for help text."""
10351047
if nargs_range[1] == constants.INFINITY:
@@ -1134,7 +1146,7 @@ def __init__(
11341146
self,
11351147
title: str,
11361148
text: RenderableType,
1137-
formatter_creator: Callable[[], Cmd2HelpFormatter],
1149+
formatter_creator: Callable[..., Cmd2HelpFormatter],
11381150
) -> None:
11391151
"""TextGroup initializer.
11401152
@@ -1258,9 +1270,9 @@ def error(self, message: str) -> NoReturn:
12581270

12591271
self.exit(2, f'{formatted_message}\n')
12601272

1261-
def _get_formatter(self) -> Cmd2HelpFormatter:
1273+
def _get_formatter(self, **kwargs: Any) -> Cmd2HelpFormatter:
12621274
"""Override with customizations for Cmd2HelpFormatter."""
1263-
return cast(Cmd2HelpFormatter, super()._get_formatter())
1275+
return cast(Cmd2HelpFormatter, super()._get_formatter(**kwargs))
12641276

12651277
def format_help(self) -> str:
12661278
"""Override to add a newline."""

0 commit comments

Comments
 (0)