Uh oh!
There was an error while loading. Please reload this page.
gh-148823: Avoid importing _colorize when creating an ArgumentParser - #148827
Conversation
This uses a new lazy import so it works with short circuiting alongside a `_colorless_theme` object to prevent the `_colorize` import if color is set to False. _theme and _decolor are now properties to prevent `_set_color` from performing the imports on creation of a formatter.
DavidCEllis
commented
Apr 21, 2026
Ah this pinged more people than I had anticipated. This appears to be due to the modification to |
| self.LAZY_IMPORTS, | ||
| ) | ||
| def test_create_parser(self): |
There was a problem hiding this comment.
Is it worth also checking .parse_args()? I think a good outcome is we only import colorize in the uncommon case where there's a parsing error or we invoke --help.
There was a problem hiding this comment.
Yes, it probably is. I'll add one. I'm fairly sure I at least tried this...
Uh oh!
There was an error while loading. Please reload this page.
Documentation build overview
97 files changed · |
JelleZijlstra
left a comment
There was a problem hiding this comment.
This mostly looks good and I'd like to get it into 3.15, but tests are failing, we're actually still importing _colorize in one of the paths.
DavidCEllis
commented
May 6, 2026
Yes, locally on this branch I'm not though which is a little confusing... All of the lazy import tests are now failing. I assume I'll have to handle the merge conflicts to figure out what's going on? |
JelleZijlstra
commented
May 6, 2026
Inline comments seem to disappear, but wanted to add that it would be nice to make it so parsing args doesn't trigger the lazy import of |
DavidCEllis
commented
May 6, 2026
The |
JelleZijlstra
commented
May 6, 2026
I feel ideally we should avoid instantiating the HelpFormatter at all but haven't looked into how hard that is. Having to inline |
DavidCEllis
commented
May 6, 2026
Ah, it's #149375 |
DavidCEllis
commented
May 6, 2026
I generally agree on avoiding the HelpFormatter, there seems to be a lot of logic that gets formatted fairly early though. |
savannahostrowski
left a comment
There was a problem hiding this comment.
Overall, this looks great!
The only thing that we may want to consider is that since _colorize is private and _ColorlessTheme exists as an unenforced parallel implementation of ThemeSection.no_colors(), it may be worth adding a small test that imports _colorize, grabs get_theme(force_no_color=True).argparse, iterates its fields, and asserts each is ""?
DavidCEllis
commented
May 6, 2026
That's fine, I can do that. I'm just trying to work out how to stop the import from being triggered due to the changes from #149375 Right now when any argument is added the help check now calls |
savannahostrowski
commented
May 6, 2026
Yes! That's what I was looking for. I had got around to turning it off just for Edit: Now how do I accept a change from a screenshot 😆 |
savannahostrowski
commented
May 6, 2026
GitHub employees, if you're listening, I have a Copilot feature request. |
Uh oh!
There was an error while loading. Please reload this page.

This is one approach to deferring the
_colorizeimport further.This makes
._themeand._decolorinto properties so that_colorizeis no longer imported when_set_coloris called, but only when the theme is accessed.Currently to prevent the import from subsequently being triggered when a subparser is created, this adds a 'fake'
_colorless_themewhich just returns empty strings for all attributes which replaces the actual colourless theme from_colorize.Effectively this is what the colourless theme from
_colorizedoes. I wanted to avoid duplicating all of the attribute names here or further complicating the logic, but there may be a better way to handle this.Testing this did require an addition to the
ensure_lazy_importstest helper to allow running additional code before checking for lazy imports. I think this might also be useful elsewhere when testing imports aren't being triggered earlier than intended.Command:
./python -c 'import argparse; argparse.ArgumentParser()'Note that this uses the new lazy imports, this would need significant tweaking if it were to be backported to 3.14.
_colorizeearlier than necessary #148823