From 95be16eb7b89c9775cfffbf6e5a5da1d3df71225 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Tue, 17 Feb 2026 03:53:51 +0100 Subject: [PATCH] [3.14] gh-144782: Make sure that ArgumentParser instances are pickleable (GH-144783) (#144895) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit gh-144782: Make sure that ArgumentParser instances are pickleable (GH-144783) (cherry picked from commit 2f7634c0291c92cf1e040fc81c4210f0883e6036) Co-authored-by: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> Co-authored-by: Bartosz Sławecki Co-authored-by: AN Long Co-authored-by: Savannah Ostrowski --- Lib/argparse.py | 10 +++++---- Lib/test/test_argparse.py | 21 +++++++++++++++++++ ...-02-13-14-20-10.gh-issue-144782.0Y8TKj.rst | 1 + 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-02-13-14-20-10.gh-issue-144782.0Y8TKj.rst diff --git a/Lib/argparse.py b/Lib/argparse.py index 1d7d34f99243..8cf856943002 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -149,6 +149,10 @@ def _copy_items(items): return copy.copy(items) +def _identity(value): + return value + + # =============== # Formatting Help # =============== @@ -200,7 +204,7 @@ class HelpFormatter(object): self._decolor = decolor else: self._theme = get_theme(force_no_color=True).argparse - self._decolor = lambda text: text + self._decolor = _identity # =============================== # Section and indentation methods @@ -1903,9 +1907,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): self._subparsers = None # register types - def identity(string): - return string - self.register('type', None, identity) + self.register('type', None, _identity) # add help argument if necessary # (using explicit default to override global argument_default) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index f48fb765bb31..8331d0218134 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -81,6 +81,27 @@ class StdStreamTest(unittest.TestCase): self.assertRegex(mocked_stderr.getvalue(), r'usage:') +class TestArgumentParserPickleable(unittest.TestCase): + + @force_not_colorized + def test_pickle_roundtrip(self): + import pickle + parser = argparse.ArgumentParser(exit_on_error=False) + parser.add_argument('--foo', type=int, default=42) + parser.add_argument('bar', nargs='?', default='baz') + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.subTest(protocol=proto): + # Try to pickle and unpickle the parser + parser2 = pickle.loads(pickle.dumps(parser, protocol=proto)) + # Check that the round-tripped parser still works + ns = parser2.parse_args(['--foo', '123', 'quux']) + self.assertEqual(ns.foo, 123) + self.assertEqual(ns.bar, 'quux') + ns2 = parser2.parse_args([]) + self.assertEqual(ns2.foo, 42) + self.assertEqual(ns2.bar, 'baz') + + class TestCase(unittest.TestCase): def setUp(self): diff --git a/Misc/NEWS.d/next/Library/2026-02-13-14-20-10.gh-issue-144782.0Y8TKj.rst b/Misc/NEWS.d/next/Library/2026-02-13-14-20-10.gh-issue-144782.0Y8TKj.rst new file mode 100644 index 000000000000..871005fd7d98 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-02-13-14-20-10.gh-issue-144782.0Y8TKj.rst @@ -0,0 +1 @@ +Fix :class:`argparse.ArgumentParser` to be :mod:`pickleable `. -- 2.47.3