Bug report
Bug description:
When debugging another problem (reported in #141570) I realized that in 734e15b, an explicit color argument of self.formatter_class initialization was replaced with a subsequent formatter._set_color(self.color) call. This was done for a good reason -- backwards compatibility.
However, it also leads to unnecessary call of HelpFormatter._set_color(True) in HelpFormatter.__init__, followed by immediate call of HelpFormatter._set_color(color).
- When color is False -- there is a needless lookup for all the environment variables as done in
_colorize.can_colorize().
- When color is True --
_colorize.can_colorize() needlessly runs twice.
Perhaps this could be optimized by:
- Replacing
HelpFormatter._set_color(color) with HelpFormatter._color = color
- Making
self._theme and self._decolor (possibly cached) properties (that change the returned value based on self._color
That way, the code would only run the potentially expensive checks when needed and only once. If this sounds lieka good idea, I can check if it's indeed that simple.
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Linked PRs
Bug report
Bug description:
When debugging another problem (reported in #141570) I realized that in 734e15b, an explicit
colorargument ofself.formatter_classinitialization was replaced with a subsequentformatter._set_color(self.color)call. This was done for a good reason -- backwards compatibility.However, it also leads to unnecessary call of
HelpFormatter._set_color(True)inHelpFormatter.__init__, followed by immediate call ofHelpFormatter._set_color(color)._colorize.can_colorize()._colorize.can_colorize()needlessly runs twice.Perhaps this could be optimized by:
HelpFormatter._set_color(color)withHelpFormatter._color = colorself._themeandself._decolor(possibly cached) properties (that change the returned value based onself._colorThat way, the code would only run the potentially expensive checks when needed and only once. If this sounds lieka good idea, I can check if it's indeed that simple.
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Linked PRs