Skip to content

Fixed crash in Python 3.15.0a6#1604

Merged
tleonhardt merged 5 commits intomainfrom
python-3.15
Mar 8, 2026
Merged

Fixed crash in Python 3.15.0a6#1604
tleonhardt merged 5 commits intomainfrom
python-3.15

Conversation

@tleonhardt
Copy link
Member

@tleonhardt tleonhardt commented Mar 7, 2026

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. Add testing for Python 3.15-dev: Start testing on Python 3.15 pre-release versions, currently 3.15.0a6

  2. 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.

  3. 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).

  4. 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.

  5. string_utils.common_prefix function added as a replacement for os.path.commonprefix which is deprecated in Python 3.15.

Closes #1603

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.
@tleonhardt tleonhardt added this to the 4.0.0 milestone Mar 7, 2026
@tleonhardt tleonhardt self-assigned this Mar 7, 2026
@tleonhardt tleonhardt added the bug label Mar 7, 2026
@codecov
Copy link

codecov bot commented Mar 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.53%. Comparing base (42d2271) to head (5c1a274).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1604      +/-   ##
==========================================
+ Coverage   99.51%   99.53%   +0.02%     
==========================================
  Files          21       21              
  Lines        4726     4743      +17     
==========================================
+ Hits         4703     4721      +18     
+ Misses         23       22       -1     
Flag Coverage Δ
unittests 99.53% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

This is a replacement for the os.path.commonprefix function which is deprecated in Python 3.15. Python 3.15 recommends using os.path.commonpath, but that isn't equivalent and yields different results.
Also:
- Fix test failure on versions of Python before 3.14
@tleonhardt tleonhardt merged commit 48b7ba1 into main Mar 8, 2026
33 checks passed
@tleonhardt tleonhardt deleted the python-3.15 branch March 8, 2026 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cmd2 crash running with Python 3.15.0a6

2 participants