This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author paul.j3
Recipients ALSchwalm, miss-islington, paul.j3, rhettinger
Date 2021-10-27.21:41:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
I should study previous posts in more detail, but here are some thoughts on correctly handling user namespace.

At the start of `parse_known_args`, there's a

        if namespace is None:
            namespace = Namespace()

We need to hang on to a copy of this namespace, e.g. call it

        import copy
        orig_namespace = copy.copy(namespace)

In _SubParsersAction.__call__, pass this copy to the subparser (instead of None):

    subnamespace, arg_strings = parser.parse_known_args(arg_strings, orig_namespace)
        for key, value in vars(subnamespace).items():
            setattr(namespace, key, value)

Prior to 9351, the main namespace was passed to the subparser

    namespace, arg_strings = parser.parse_known_args(arg_strings, namespace)

The trick is to get orig_namespace from the main parse_known_args to SubParsersAction.__call__ method.

in a 9351 post I explore the idea of allowing the user to specify a 'sub_namespace' for the subparser.

In any case, this is a complicated issue that needs careful thought and more extensive testing.  I didn't entirely like the original 9351 change, but since that's been part of argparse for many years, we need to very careful about clobbering it.
Date User Action Args
2021-10-27 21:41:36paul.j3setrecipients: + paul.j3, rhettinger, miss-islington, ALSchwalm
2021-10-27 21:41:36paul.j3setmessageid: <>
2021-10-27 21:41:36paul.j3linkissue45235 messages
2021-10-27 21:41:36paul.j3create