https://github.com/python/cpython/commit/37c6dd1c91159c87bb458c5c2e21f02a88f9386b
commit: 37c6dd1c91159c87bb458c5c2e21f02a88f9386b
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: savannahostrowski <[email protected]>
date: 2025-12-05T17:14:56Z
summary:

[3.14] GH-142267: Cache formatter to avoid repeated `_set_color` calls 
(GH-142268) (#142313)

GH-142267: Cache formatter to avoid repeated `_set_color` calls (GH-142268)
(cherry picked from commit 4085ff7b32f91bad7d821e5564d8565c5928f7d1)

Co-authored-by: Savannah Ostrowski <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-12-04-23-26-12.gh-issue-142267.yOM6fP.rst
M Lib/argparse.py

diff --git a/Lib/argparse.py b/Lib/argparse.py
index 88c1f5a7ef3342..2a74bf27b9d623 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1552,8 +1552,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:
@@ -1741,8 +1741,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:
@@ -1897,6 +1897,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'))
@@ -2728,6 +2731,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 slow _set_color 
calls.
+        if self._cached_formatter is None:
+            self._cached_formatter = self._get_formatter()
+        return self._cached_formatter
+
     # =====================
     # Help-printing methods
     # =====================
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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to