gh-155043: Support copy.replace() for argparse.Namespace (#155049)
savannahostrowski <[email protected]> Wed, 05 Aug 2026 12:14:11 -0400 (EDT)
| Newsgroups | gmane.comp.python.cvs |
|---|---|
| Message-ID | <[email protected]> |
https://github.com/python/cpython/commit/9dc11d887dacd4370e106f677d0649b100452e7d commit: 9dc11d887dacd4370e106f677d0649b100452e7d branch: main author: Serhiy Storchaka <[email protected]> committer: savannahostrowski <[email protected]> date: 2026-08-05T09:14:01-07:00 summary: gh-155043: Support copy.replace() for argparse.Namespace (#155049) files: A Misc/NEWS.d/next/Library/2026-08-01-19-04-00.gh-issue-155043.Ar1Nsp.rst M Doc/library/argparse.rst M Lib/argparse.py M Lib/test/test_argparse.py diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index e4a5f4d109b499..8ae96311026f33 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -1679,6 +1679,12 @@ The Namespace object Simple class used by default by :meth:`~ArgumentParser.parse_args` to create an object holding attributes and return it. + :class:`!Namespace` objects support :func:`copy.replace`, + which returns a copy of the object with the specified attributes replaced. + + .. versionchanged:: next + Added support for :func:`copy.replace`. + This class is deliberately simple, just an :class:`object` subclass with a readable string representation. If you prefer to have dict-like view of the attributes, you can use the standard Python idiom, :func:`vars`:: diff --git a/Lib/argparse.py b/Lib/argparse.py index 25994d7a389efa..fe9fde7f65830f 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1545,6 +1545,12 @@ def __eq__(self, other): def __contains__(self, key): return key in self.__dict__ + def __replace__(self, /, **changes): + new = self.__class__() + new.__dict__.update(self.__dict__) + new.__dict__.update(changes) + return new + class _ActionsContainer(object): diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index ae02cd11804e50..950671e4395cd4 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2,6 +2,7 @@ import _colorize import contextlib +import copy import functools import io import operator @@ -6267,6 +6268,19 @@ def test_equality_returns_notimplemented(self): self.assertIs(ns.__eq__(None), NotImplemented) self.assertIs(ns.__ne__(None), NotImplemented) + def test_replace(self): + ns = argparse.Namespace(a=1, b=2) + new = copy.replace(ns, b=3, c=4) + self.assertIsInstance(new, argparse.Namespace) + self.assertEqual(new, argparse.Namespace(a=1, b=3, c=4)) + self.assertEqual(ns, argparse.Namespace(a=1, b=2)) + + class MyNamespace(argparse.Namespace): + pass + new = copy.replace(MyNamespace(a=1), a=2) + self.assertIsInstance(new, MyNamespace) + self.assertEqual(new.a, 2) + # =================== # File encoding tests diff --git a/Misc/NEWS.d/next/Library/2026-08-01-19-04-00.gh-issue-155043.Ar1Nsp.rst b/Misc/NEWS.d/next/Library/2026-08-01-19-04-00.gh-issue-155043.Ar1Nsp.rst new file mode 100644 index 00000000000000..715b0a1783730e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-01-19-04-00.gh-issue-155043.Ar1Nsp.rst @@ -0,0 +1 @@ +:class:`argparse.Namespace` objects now support :func:`copy.replace`. _______________________________________________ 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]