Skip to content

Commit 4fa408e

Browse files
committed
Lazily import _colorize
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.
1 parent 629dd53 commit 4fa408e

1 file changed

Lines changed: 40 additions & 12 deletions

File tree

Lib/argparse.py

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
from gettext import gettext as _
9393
from gettext import ngettext
9494

95+
lazy import _colorize
96+
9597
SUPPRESS = '==SUPPRESS=='
9698

9799
OPTIONAL = '?'
@@ -156,6 +158,15 @@ def _identity(value):
156158
# Formatting Help
157159
# ===============
158160

161+
class _ColorlessTheme:
162+
# A 'fake' theme for no colors
163+
def __getattr__(self, name):
164+
# _colorize's no_color themes are just all empty strings
165+
# by directly using empty strings the import is avoided
166+
return ""
167+
168+
_colorless_theme = _ColorlessTheme()
169+
159170

160171
class HelpFormatter(object):
161172
"""Formatter for generating usage messages and argument help strings.
@@ -196,14 +207,32 @@ def __init__(
196207
self._set_color(False)
197208

198209
def _set_color(self, color, *, file=None):
199-
from _colorize import can_colorize, decolor, get_theme
200-
201-
if color and can_colorize(file=file):
202-
self._theme = get_theme(force_color=True).argparse
203-
self._decolor = decolor
210+
# Set a new color setting and file, clear caches for theme and decolor
211+
self._theme_color = color
212+
self._theme_file = file
213+
self._cached_theme = None
214+
self._cached_decolor = None
215+
216+
def _get_theme_and_decolor(self):
217+
# If self._theme_color is false, this prevents _colorize from importing
218+
if self._theme_color and _colorize.can_colorize(file=self._theme_file):
219+
self._cached_theme = _colorize.get_theme(force_color=True).argparse
220+
self._cached_decolor = _colorize.decolor
204221
else:
205-
self._theme = get_theme(force_no_color=True).argparse
206-
self._decolor = _identity
222+
self._cached_theme = _colorless_theme
223+
self._cached_decolor = _identity
224+
225+
@property
226+
def _theme(self):
227+
if self._cached_theme is None:
228+
self._get_theme_and_decolor()
229+
return self._cached_theme
230+
231+
@property
232+
def _decolor(self):
233+
if self._cached_decolor is None:
234+
self._get_theme_and_decolor()
235+
return self._cached_decolor
207236

208237
# ===============================
209238
# Section and indentation methods
@@ -2856,12 +2885,11 @@ def _print_message(self, message, file=None):
28562885
pass
28572886

28582887
def _get_theme(self, file=None):
2859-
from _colorize import can_colorize, get_theme
2860-
2861-
if self.color and can_colorize(file=file):
2862-
return get_theme(force_color=True).argparse
2888+
# If self.color is False, _colorize is not imported
2889+
if self.color and _colorize.can_colorize(file=file):
2890+
return _colorize.get_theme(force_color=True).argparse
28632891
else:
2864-
return get_theme(force_no_color=True).argparse
2892+
return _colorless_theme
28652893

28662894
# ===============
28672895
# Exiting methods

0 commit comments

Comments
 (0)