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`::
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):
import _colorize
import contextlib
+import copy
import functools
import io
import operator
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