Skip to content

Conversation

@savannahostrowski
Copy link
Member

@savannahostrowski savannahostrowski commented Dec 4, 2025

After digging into this, I think that we should just remove color from being passed into HelpFormatter, since this never worked and was never documented. IMO, this should be controlled by the parser anyway.

@hugovk
Copy link
Member

hugovk commented Dec 5, 2025

(Updated from main to fix the Android failure: #142289)

savannahostrowski and others added 2 commits December 5, 2025 07:48
…BfsD4.rst

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@savannahostrowski savannahostrowski enabled auto-merge (squash) December 5, 2025 15:48
@savannahostrowski savannahostrowski merged commit 4b14529 into python:main Dec 5, 2025
46 checks passed
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@hamdanal
Copy link
Contributor

hamdanal commented Dec 7, 2025

This PR broke instantiating the formatter outside the parser because _theme is no longer set in __init__ as _set_color is no longer called.

import argparse

formatter = argparse.HelpFormatter(prog="program")
formatter.add_usage(usage=None, actions=[], groups=[])
print(formatter.format_help())

This now raises an AttributeError:

Traceback (most recent call last):
  File "/tmp/t.py", line 5, in <module>
    print(formatter.format_help())
          ~~~~~~~~~~~~~~~~~~~~~^^
  File "/tmp/argparse.py", line 303, in format_help
    help = self._root_section.format_help()
  File "/tmp/argparse.py", line 228, in format_help
    item_help = join([func(*args) for func, args in self.items])
                      ~~~~^^^^^^^
  File "/tmp/argparse.py", line 315, in _format_usage
    t = self._theme
        ^^^^^^^^^^^
AttributeError: 'HelpFormatter' object has no attribute '_theme'

This only broke a test over at https://github.com/hamdanal/rich-argparse and I am not sure if it a supported usage of the formatter. Let me know if I need to create an issue/PR.

@savannahostrowski
Copy link
Member Author

Hey @hamdanal, thanks for raising this! Direct instantiation of HelpFormatter should continue to work. I think the fix is to call self._set_color(False) at the end of __init__ to initialize _theme with a no-color default. The parser can still override it afterward. I've put up #142384 to address this.

@hugovk
Copy link
Member

hugovk commented Dec 7, 2025

@hamdanal And thank you for testing the nightly and reporting so quickly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants