gh-148823: Avoid importing _colorize when creating an ArgumentParser#148827
Open
DavidCEllis wants to merge 4 commits intopython:mainfrom
Open
gh-148823: Avoid importing _colorize when creating an ArgumentParser#148827DavidCEllis wants to merge 4 commits intopython:mainfrom
_colorize when creating an ArgumentParser#148827DavidCEllis wants to merge 4 commits intopython:mainfrom
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.
Contributor
Author
|
Ah this pinged more people than I had anticipated. This appears to be due to the modification to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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