From 3db10c57444dacf74dbc263b2795e92a1491175d Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Thu, 4 Dec 2025 12:23:34 -0800 Subject: [PATCH 1/3] Cache validation formatter to avoid duplicative get_formatter --- Lib/argparse.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index 55ecdadd8c9398..ba27d24f630768 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1,5 +1,4 @@ # Author: Steven J. Bethard . -# New maintainer as of 29 August 2019: Raymond Hettinger """Command-line parsing library @@ -1570,8 +1569,8 @@ def add_argument(self, *args, **kwargs): f'instance of it must be passed') # raise an error if the metavar does not match the type - if hasattr(self, "_get_formatter"): - formatter = self._get_formatter() + if hasattr(self, "_get_validation_formatter"): + formatter = self._get_validation_formatter() try: formatter._format_args(action, None) except TypeError: @@ -1765,8 +1764,8 @@ def _handle_conflict_resolve(self, action, conflicting_actions): action.container._remove_action(action) def _check_help(self, action): - if action.help and hasattr(self, "_get_formatter"): - formatter = self._get_formatter() + if action.help and hasattr(self, "_get_validation_formatter"): + formatter = self._get_validation_formatter() try: formatter._expand_help(action) except (ValueError, TypeError, KeyError) as exc: @@ -1921,6 +1920,9 @@ def __init__(self, self.suggest_on_error = suggest_on_error self.color = color + # Cached formatter for validation (avoids repeated _set_color calls) + self._cached_formatter = None + add_group = self.add_argument_group self._positionals = add_group(_('positional arguments')) self._optionals = add_group(_('options')) @@ -2752,6 +2754,13 @@ def _get_formatter(self): formatter._set_color(self.color) return formatter + def _get_validation_formatter(self): + # Return cached formatter for read-only validation operations + # (_expand_help and _format_args). Avoids repeated _set_color calls. + if self._cached_formatter is None: + self._cached_formatter = self._get_formatter() + return self._cached_formatter + # ===================== # Help-printing methods # ===================== From 825c4474f1b9e67a6e9fe5bec83526bb585992b4 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 4 Dec 2025 23:26:15 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-12-04-23-26-12.gh-issue-142267.yOM6fP.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-12-04-23-26-12.gh-issue-142267.yOM6fP.rst diff --git a/Misc/NEWS.d/next/Library/2025-12-04-23-26-12.gh-issue-142267.yOM6fP.rst b/Misc/NEWS.d/next/Library/2025-12-04-23-26-12.gh-issue-142267.yOM6fP.rst new file mode 100644 index 00000000000000..f46e82105fc2f5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-12-04-23-26-12.gh-issue-142267.yOM6fP.rst @@ -0,0 +1 @@ +Improve :mod:`argparse` performance by caching the formatter used for argument validation. From c8959cfab5e2255d81fa38530904c4df38c182a5 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Fri, 5 Dec 2025 07:55:28 -0800 Subject: [PATCH 3/3] PR review fixes --- Lib/argparse.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index ba27d24f630768..e6661839c2e290 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1,4 +1,5 @@ # Author: Steven J. Bethard . +# New maintainer as of 29 August 2019: Raymond Hettinger """Command-line parsing library @@ -2756,7 +2757,7 @@ def _get_formatter(self): def _get_validation_formatter(self): # Return cached formatter for read-only validation operations - # (_expand_help and _format_args). Avoids repeated _set_color calls. + # (_expand_help and _format_args). Avoids repeated slow _set_color calls. if self._cached_formatter is None: self._cached_formatter = self._get_formatter() return self._cached_formatter