Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparse set_defaults on subcommands should override top level set_defaults #53597

Open
bethard mannequin opened this issue Jul 23, 2010 · 21 comments
Open

argparse set_defaults on subcommands should override top level set_defaults #53597

bethard mannequin opened this issue Jul 23, 2010 · 21 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@bethard
Copy link
Mannequin

bethard mannequin commented Jul 23, 2010

BPO 9351
Nosy @ezio-melotti, @bitdancer, @akheron, @remram44, @shihai1991
Files
  • issue9351.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2010-07-23.14:06:50.789>
    labels = ['type-bug', 'library']
    title = 'argparse set_defaults on subcommands should override top level set_defaults'
    updated_at = <Date 2021-10-27.21:44:47.104>
    user = 'https://bugs.python.org/bethard'

    bugs.python.org fields:

    activity = <Date 2021-10-27.21:44:47.104>
    actor = 'paul.j3'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2010-07-23.14:06:50.789>
    creator = 'bethard'
    dependencies = []
    files = ['27795']
    hgrepos = []
    issue_num = 9351
    keywords = ['patch']
    message_count = 21.0
    messages = ['111324', '173602', '174147', '174206', '174208', '206993', '219332', '229619', '229620', '229968', '229983', '230056', '230084', '233041', '244768', '244786', '274369', '342154', '350638', '373665', '405130']
    nosy_count = 12.0
    nosy_names = ['bethard', 'ezio.melotti', 'r.david.murray', 'nailor', 'python-dev', 'petri.lehtinen', 'paul.j3', 'mikn', 'remram', 'Changaco', 'smparkes', 'shihai1991']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'needs patch'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue9351'
    versions = ['Python 2.7', 'Python 3.4', 'Python 3.5']

    @bethard
    Copy link
    Mannequin Author

    bethard mannequin commented Jul 23, 2010

    If you use set_defaults on a subparser, but a default exists on the top level parser, the subparser defaults are ignored:

    >>> parser = argparse.ArgumentParser()
    >>> xparser = parser.add_subparsers().add_parser('X')
    >>> parser.set_defaults(foo=1)
    >>> xparser.set_defaults(foo=2)
    >>> parser.parse_args(['X'])
    Namespace(foo=1)

    This is counter to what people probably expect, that the subparser, when selected, would override the top level parser.

    The behavior is because of the following code in parse_known_args:

            for dest in self._defaults:
                if not hasattr(namespace, dest):
                    setattr(namespace, dest, self._defaults[dest])

    This happens before the subparser sees the namespace object, and so the subparser sees that no defaults need to be filled in.

    @bethard bethard mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jul 23, 2010
    @nailor
    Copy link
    Mannequin

    nailor mannequin commented Oct 23, 2012

    I've attached a proposed fix where the subparser parses to an empty namespace and that namespace is then merged with the parent namespace

    @akheron
    Copy link
    Member

    akheron commented Oct 29, 2012

    + for key in vars(subnamespace):
    + setattr(namespace, key, getattr(subnamespace, key))

    There might be even more clever ways to achieve this, but what about at least saying "for key, value in vars(subnamespace).items()", and then using the value accordingly, to avoid the getattr() call?

    @nailor
    Copy link
    Mannequin

    nailor mannequin commented Oct 30, 2012

    Yeah, I tried figuring out something more clever, as this, in the current form, has a bit too hackish feeling in it, but I couldn't find a proper tool for the job.

    Anyway, attached a patch with the getattr removed.

    @akheron
    Copy link
    Member

    akheron commented Oct 30, 2012

    LGTM. Steven?

    @mikn
    Copy link
    Mannequin

    mikn mannequin commented Dec 27, 2013

    Just wanted to drop in here to let you know that I hit this behaviour recently when writing a small tool using both configparser and argparse and the workaround proves rather annoying (custom namespace object or similar).

    Would be awesome if this moved forward with the proposed patch!

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented May 29, 2014

    This is not getting much attention for several reasons:

    • there's quite a backlog of argparse patches and issues

    • 'set_defaults' is not commonly used. Setting default in 'add_argument' seems more common.

    • defining the same argument for both the parser and subparser can create other difficulties.

    • in Bethard's example, 'set_default' sets an attribute that has no connection to any argument. It's a cleaver trick that few users will think to use, and probably won't be of much value.

    The idea proposed here of using a subnamespace for the subparser is interesting. It reminds me of a StackOverflow question about implementing nested namespaces.

    http://stackoverflow.com/questions/18668227

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 18, 2014

    New changeset e9cb45ccf42b by R David Murray in branch '3.4':
    bpo-9351: set_defaults on subparser is no longer ignored if set on parent.
    https://hg.python.org/cpython/rev/e9cb45ccf42b

    New changeset b35a811d4420 by R David Murray in branch 'default':
    Merge: bpo-9351: set_defaults on subparser is no longer ignored if set on parent.
    https://hg.python.org/cpython/rev/b35a811d4420

    New changeset 1a3143752db2 by R David Murray in branch '2.7':
    bpo-9351: set_defaults on subparser is no longer ignored if set on parent.
    https://hg.python.org/cpython/rev/1a3143752db2

    @bitdancer
    Copy link
    Member

    Thanks, Jyrki.

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Oct 24, 2014

    A possible problem with this patch is that it overrides any Namespace given by the user. In the test example:

        parser.parse_args(['X'], namespace=argparse.Namespace(foo=3))

    the result is 'foo=2', the setdefault from the subparser. Previously, and with a single level parser, the result would be 'foo=3'. This is also true for an ordinary argument default in the subparser.

    It could be argued that a user is unlikely to use both the 'namespace=...' parameter, and 'setdefault' for the same variable, especially one that isn't an argument 'dest'. But the fact that the custom namespace does not override regular subparser argument defaults bothers me.

    Also, should an untested change like this be applied to 3.4 and 2.7? This is not a backward compatible bug fix.

    The test case also touches on a real bug in the recent releases - subparsers are now optional. 'parser.parse_args([])' runs without complaint about the missing 'X'. Actually it is convenient for testing this case. But it is still an incompatibility with earlier behavior

    http://bugs.python.org/issue9253

    @bitdancer
    Copy link
    Member

    Yeah, I hesitated a bit about the backports, but didn't visualize the scenario you are suggesting and thought it would be safe. Perhaps it should be backed out of 2.7 and 3.4.

    For 3.5, do you have any thoughts about how to make namespace= play nicely with this problem?

    @bitdancer bitdancer reopened this Oct 25, 2014
    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Oct 27, 2014

    I'm exploring modifying that patch by adding a 'subnamespace' attribute to the subparsers Action object. This would give the user, or the main parser control over the namespace that is passed to a subparser.

    For example:

            subnamespace = getattr(self, 'subnamespace', False)
            if subnamespace is None:
                pass
            elif not  subnamespace:
                subnamespace = namespace
            # if None, subparser will use a fresh one
            subnamespace, arg_strings = parser.parse_known_args(arg_strings, subnamespace)
            for key, value in vars(subnamespace).items():
                setattr(namespace, key, value)

    As written here, no value (or any False) would use the main parser namespace, as done with existing code.

    A 'None' value, would use the a new empty namespace, as proposed in this patch.

    Or the user could directly define the namespace, e.g.

        sp = parser.add_subparsers()
        sp.subnamespace = Namespace(foo=4)

    That could also be convenient if the user is using a custom Namespace class.

    'parse_known_args' could also set this attribute, based on whether the user has given it a namespace or not.

    @bitdancer
    Copy link
    Member

    If I understand you correctly, that would mean that if the namespace keyword is not used, we'd have the fixed behavior, but if the namespace keyword is used, we'd have the backward compatible behavior? If I'm understanding correctly, that sounds like a good solution to me (coupled with backing this out of the maint versions).

    @Changaco
    Copy link
    Mannequin

    Changaco mannequin commented Dec 23, 2014

    The broken patch made it into the 2.7.9 release, causing argparse to silently ignore arguments, cf http://bugs.python.org/issue23058

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Jun 3, 2015

    Another example of this patch causing backward compatibility problems

    http://bugs.python.org/issue24251

    @remram44
    Copy link
    Mannequin

    remram44 mannequin commented Jun 3, 2015

    To me this is much more than a compatibility problem. The way it worked before made a lot of sense, and just felt like the "correct" solution to accept a flag in multiple places.

    Having a --verbose flag is something everybody should consider (Python has a decent builtin logging module), and anybody providing it would definitely want to accept it before and after subcommands (or at least, for every subcommand).

    The only way right now is to not only create different arguments with add_argument(), for each parser, but you also need to provide different destination names (and then do something shitty like verbosity = args.verb_main+args.verb_subcommand). This bug makes argparse completely unusable for any real-life application that uses subparsers (in addition to breaking existing programs). And it breaks silently too, simply amazing!

    Of course there is very little point in fixing this now. Since this affects multiple released versions of Python, I have to use a work-around anyway (until I can move from argparse to something that won't decide to break someday for the hell of it).

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Sep 4, 2016

    In http://bugs.python.org/issue27859 I've demonstrated how a subclass of _SubParsersAction can be used to revert the behavior to pre 2.7.9, passing the main namespace to the subparser.

    The only other change is to the parser registry:

        parser.register('action', 'parsers', MyParserAction)

    So there's no need to change the argparse.py file itself.

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented May 11, 2019

    A variation on the problem I reported in

    https://bugs.python.org/issue9351#msg229968

    is that a custom Namespace class as documented in

    https://docs.python.org/3/library/argparse.html#the-namespace-object

    will not be used by the subparsers. Only the main parser uses that custom Namespace object; the subparsers continue to use the default 'argparse.Namespace()'.

    @shihai1991
    Copy link
    Member

    How about use a flag(such USING_OUT_NAMESPACE) to identify we use namespace or not?

    For example:
    subnamespace, arg_strings = parser.parse_known_args(arg_strings, None)
    for key, value in vars(subnamespace).items():
    if USING_OUT_NAMESPACE and not hasattr(namespace, key):
    setattr(namespace, key, value)

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Jul 15, 2020

    I just realized if the subparser argument used

    default=argparse.SUPPRESS

    it would not clobber values (default or user) set by the main parser.

    (a 'store_true' would have to be replaced with a 'store_const' to do this)

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Oct 27, 2021

    A new patch, https://bugs.python.org/issue45235 has clobbered this patch.

    It has also exposed the inadequate unittesting for the case(s) where the 'dest' of main namespace, subparser namespace, user provided namespace overlap.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    Status: Bugs
    Development

    No branches or pull requests

    3 participants