Visitar URL original
argparse: add extend_const action · Issue #87326 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20230328113125/https://github.com/python/cpython/issues/87326
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: add extend_const action #87326

Open
roganartu mannequin opened this issue Feb 8, 2021 · 4 comments
Open

argparse: add extend_const action #87326

roganartu mannequin opened this issue Feb 8, 2021 · 4 comments
Labels
3.10 bug and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@roganartu
Copy link
Mannequin

roganartu mannequin commented Feb 8, 2021

BPO 43160
Nosy @rhettinger, @roganartu
PRs
  • bpo-43160: Add extend_const action to argparse #24478
  • 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 2021-02-08.04:12:31.886>
    labels = ['type-feature', 'library', '3.10']
    title = 'argparse: add extend_const action'
    updated_at = <Date 2021-02-09.07:54:04.617>
    user = 'https://github.com/roganartu'

    bugs.python.org fields:

    activity = <Date 2021-02-09.07:54:04.617>
    actor = 'roganartu'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-02-08.04:12:31.886>
    creator = 'roganartu'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43160
    keywords = ['patch']
    message_count = 4.0
    messages = ['386614', '386690', '386696', '386697']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'paul.j3', 'roganartu']
    pr_nums = ['24478']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue43160'
    versions = ['Python 3.10']

    @roganartu
    Copy link
    Mannequin Author

    roganartu mannequin commented Feb 8, 2021

    I submitted this to the python-ideas mailing list early last year: https://mail.python.org/archives/list/python-ideas@python.org/thread/7ZHY7HFFQHIX3YWWCIJTNB4DRG2NQDOV/. Recently I had some time to implement it (it actually turned out to be pretty trivial), so thought I'd put forward a PR.

    Here's the summary from the mailing list submission:

    I have found myself a few times in a position where I have a repeated argument that uses the append action, along with some convenience arguments that append a specific const to that same dest (eg: --filter-x being made equivalent to --filter x via append_const). This is particularly useful in cli apps that expose some kind of powerful-but-verbose filtering capability, while also providing shorter aliases for common invocations. I'm sure there are other use cases, but this is the one I'm most familiar with.

    The natural extension to this filtering idea are convenience args that set two const values (eg: --filter x --filter y being equivalent to --filter-x-y), but there is no extend_const action to enable this.

    While this is possible (and rather straight forward) to add via a custom action, I feel like this should be a built-in action instead. append has append_const, it seems intuitive and reasonable to expect extend to have extend_const too (my anecdotal experience the first time I came across this need was that I simply tried using extend_const without checking the docs, assuming it already existed).

    Here's an excerpt from the docs I drafted for this addition that hopefully convey the intent and use case clearly.

    +* ``'extend_const'`` - This stores a list, and extends each argument value to the list.
    +  The ``'extend_const'`` action is typically useful when you want to provide an alias
    +  that is the combination of multiple other arguments. For example::
    +
    +    >>> parser = argparse.ArgumentParser()
    +    >>> parser.add_argument('--str', dest='types', action='append_const', const=str)
    +    >>> parser.add_argument('--int', dest='types', action='append_const', const=int)
    +    >>> parser.add_argument('--both', dest='types', action='extend_const', const=(str, int))
    +    >>> parser.parse_args('--str --int'.split())
    +    Namespace(types=[<class 'str'>, <class 'int'>])
    +    >>> parser.parse_args('--both'.split())
    +    Namespace(types=[<class 'str'>, <class 'int'>])

    @roganartu roganartu mannequin added 3.10 bug and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Feb 8, 2021
    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Feb 9, 2021

    It's not clear what your patch does that the existing 'store_const' doesn't. Even 'append_const' does the same except for a extra level of list/tuple nesting.

    But I'll admit that I didn't much need for 'extend' addition, but others, including the original developer liked it.

    I suspect users of your addition will get a surprise if they aren't careful to provide a list or tuple 'const':

        const='foobar'
        const=str

    @roganartu
    Copy link
    Mannequin Author

    roganartu mannequin commented Feb 9, 2021

    Perhaps the example I added to the docs isn't clear enough and should be changed because you're right, that specific one can be served by store_const. Turns out coming up with examples that are minimal but not too contrived is hard! Let me try again with a longer example that hopefully shows more clearly how the existing action's behaviours differ from my patch.

        parser = argparse.ArgumentParser()
        parser.add_argument("--foo", action="append", default=[])
        parser.add_argument("--append", action="append_const", dest="foo", const=["a", "b"])
        parser.add_argument("--store", action="store_const", dest="foo", const=["a", "b"])

    When run on master the following behaviour is observed:

    --foo a --foo b --foo c
            Namespace(foo=['a', 'b', 'c'])
    --foo c --append
            Namespace(foo=['c', ['a', 'b']])
    --foo c --store
            Namespace(foo=['a', 'b'])
    --store --foo a
            Namespace(foo=['a', 'b', 'c'])
    

    If we then add the following:

        parser.add_argument("--extend", action="extend_const", dest="foo", const=["a", "b"])

    and then run it with my patch the following can be observed:

    --foo c --extend
            Namespace(foo=['c', 'a', 'b'])
    --extend --foo c
            Namespace(foo=['a', 'b', 'c'])
    

    store_const is actually a pretty close fit, but the way it makes order significant (specifically in that it will silently drop prev values) seems like it'd be rather surprising to users and makes it a big enough footgun for this use case that I don't think it's a satisfactory alternative.

    I suspect users of your addition will get a surprise if they aren't careful to provide a list or tuple 'const'

    I did consider that, but I don't think they'd get any more of a surprise than for doing the same with list.extend vs list.append.

    @roganartu
    Copy link
    Mannequin Author

    roganartu mannequin commented Feb 9, 2021

    Sorry, there's a typo in my last comment.

    --store --foo a
            Namespace(foo=['a', 'b', 'c'])
    

    from the first set of examples should have been

    --store --foo c
            Namespace(foo=['a', 'b', 'c'])
    

    @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
    3.10 bug and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    Status: Features
    Development

    No branches or pull requests

    0 participants