Skip to content

Conversation

@chillakalyan
Copy link
Contributor

This PR fixes a redundant color initialization in argparse.HelpFormatter.

Currently, HelpFormatter.__init__() calls _set_color(color) immediately,
and then _get_formatter() calls _set_color(self.color) again.

This results in duplicate environment checks and redundant calls to
_colorize.can_colorize(), especially when color=False.

This patch removes the early _set_color(color) call in __init__ and instead
stores the value in self._color. The actual color initialization is now
performed only once in _get_formatter(), preserving existing behavior while
avoiding duplicate work.

A NEWS entry for gh-141571 has been added.

width -= 2

self._set_color(color)
self._color = color
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand where is this used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!
You're right — after checking the code path again, this self._color assignment is not actually used anywhere before _get_formatter() runs. I will check and i will report if i found the real issue

@picnixz
Copy link
Member

picnixz commented Nov 16, 2025

Why are you re-opening the same PR with same (incorrect) fix? please only open a PR when you've investigated and fixed the behavior. If you do not have a fix, please close the PR yourself (or I'll do so in a few days)

@picnixz picnixz closed this Nov 16, 2025
@picnixz picnixz reopened this Nov 16, 2025
@picnixz picnixz marked this pull request as draft November 16, 2025 10:21
@picnixz
Copy link
Member

picnixz commented Dec 6, 2025

Closing because this was fixed by another PR.

@picnixz picnixz closed this Dec 6, 2025
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