Skip to content

argparse.ArgumentParser instances are not pickleable #144782

Closed

Description

@mauvilsa

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

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Feb 13, 2026
  2. mpkocher commented on Feb 13, 2026

    @mpkocher
    Contributor

    I think this might run into challenges depending on how people write the parser instance setup or "glue" around it.

    Is this supposed to work?

    p = ArgumentParser()
    p.add_argument('-n', '--name', type=lambda x: x.upper(), required=True)
    pickle.dumps(p)

    Perhaps the argparse part might improved, but how people hook in to argparse, or put the glue around It will be fragile.

    For example, I suspect patterns or styles like this will never work (and shouldn't be expected to be pickleable).

  3. mauvilsa commented on Feb 13, 2026

    @mauvilsa
    ContributorAuthor

    This issue doesn't suggest that all possible parsers should be pickleable. But it should be possible to build parsers with the intention of being pickleable.

    A parser that is not pickleable because of a lambda in an add_argument is not a blocker. The problematic add_argument can be fixed in each project if needed. However, argparse.ArgumentParser itself not being pickleable, e.g. due to the new _set_color.<locals>.<lambda>, is a blocker. Multiprocessing in pytorch-lightning has been working for years without any issue thanks to the self.register('type', None, _identity) workaround. But now with Python 3.14.3 this is broken.

  4. added a commit that references this issue on Feb 17, 2026
  5. added a commit that references this issue on Feb 17, 2026
  6. added a commit that references this issue on Feb 17, 2026
  7. moved this from Bugs to Doc issues in Argparse issueson Feb 17, 2026
  8. mpkocher commented on Feb 17, 2026

    @mpkocher
    Contributor

    The Argparse parser instance doesn't strike me as something that should be pickleable. It's a very imperative messy stateful thing (e.g. colorization), I'm not sure the value proposition is really worth adding a new requirement and all that goes with that.

    What is the expected behavior here?

    1. Create argparse instance and pickle it to a file
    2. export NO_COLOR to false, load picke file in a different process.

    Also, if you're adding this feature, I believe there needs to be (at a minimum) a smoke test for the pickling of the subparser case.

  9. added a commit that references this issue on Feb 28, 2026
  10. added a commit that references this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions