mauvilsa · GitHub

Bug report

Bug description:

argparse.ArgumentParser instances are not pickleable, and being pickleable is necessary for multiprocessing to work. For example pytorch-lightning sends ArgumentParser objects between processes.

Currently the following fails:

import argparse, pickle
parser = argparse.ArgumentParser()
pickle.loads(pickle.dumps(parser))

giving error:

_pickle.PicklingError: Can't pickle local object <function ArgumentParser.__init__.<locals>.identity

There is the following workaround that is being used in jsonargparse:

import argparse, pickle
def _identity(value):
    return value
class ArgumentParser(argparse.ArgumentParser):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.register('type', None, _identity)
parser = ArgumentParser()
pickle.loads(pickle.dumps(parser))

Even though the workaround solves the issue, there is no guarantee that changes in argparse make parsers unpickleable due to other reasons. For instance on Python 3.14.3 we are seeing the error:

_pickle.PicklingError: Can't pickle local object <function HelpFormatter._set_color.<locals>.<lambda>

It would be preferable that ArgumentParser is guaranteed to be pickleable here in cpython.

CPython versions tested on:

3.9, 3.10, 3.11, 3.12, 3.13, 3.14, main

Operating systems tested on:

Linux

Linked PRs

Read the original on github.com ↗