return copy.copy(items)
+def _identity(value):
+ return value
+
+
# ===============
# Formatting Help
# ===============
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
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)
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):