Skip to content
10 changes: 6 additions & 4 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ def _copy_items(items):
return copy.copy(items)


def _identity(value):
return value


# ===============
# Formatting Help
# ===============
Expand Down Expand Up @@ -199,7 +203,7 @@ def _set_color(self, color, *, file=None):
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
Expand Down Expand Up @@ -1981,9 +1985,7 @@ def __init__(self,
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)
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ def test_skip_invalid_stdout(self):
self.assertRegex(mocked_stderr.getvalue(), r'usage:')


class TestArgumentParserPickleable(unittest.TestCase):

@force_not_colorized
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work with color?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this fix, currently it does work with color. Should we also test with color?

I agree that it is difficult to guarantee pickleablility, but running all existing tests with pickle seems unnecessary. What would make a parser unpickleable is just assigning an unpickleable object. So only need one test for each path of the code where objects could be assigned. Instead of more tests could also be part of the review process, watching out for locally defined functions and lambdas.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add tests for each concrete case after we found that it breaks pickle. But we cannot guarantee that future changes (maybe in other part of the stdlib) will not break it for not tested cases.

For example, currently it works with color. But the color machinery is outside of the scope of the argparse module. So, the test with color is necessary, without it we cannot catch regression.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I understand. I can add the test with color. I can also analyze the code a bit to see if there are more tests cases that seem important to prevent regressions. Apart from this, is there any way for me to get notified when an argparse pull request in cpython is ready for review? I can be the one looking out for changes that could make parsers unpickleable.

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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :class:`argparse.ArgumentParser` to be :mod:`pickleable <pickle>`.
Loading