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.

classification
Title: Can argparse._AttributeHolder._get_kwargs become a public API?
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: bethard, dsuch, eric.araujo, paul.j3, rhettinger, tim.golden
Priority: normal Keywords:

Created on 2010-10-25 04:44 by dsuch, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg119539 - (view) Author: Dariusz Suchojad (dsuch) Date: 2010-10-25 04:44
Hello,

I was wondering if it were possible for the argparse._AttributeHolder._get_kwargs to become a part of the public API.

Using this method is a very convenient way to get a hold of the arguments provided by the user and it would be shame to keep it private, I for one use it in several places even though I clearly know the name starts with an underscore, it's just that reimplementing it in my code seems counter-productive, would be very nice if '_get_kwargs' became 'get_kwargs' in some future release.

Thanks for considering it!
msg119542 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2010-10-25 07:34
Could you elaborate a little on what you use it for? The argparse module only uses this for pretty __repr__ on the various objects. (And in fact, it looks like it's gotten a little out of sync - "required" is missing from Action, and a number of things are missing from ArgumentParser.)
msg119543 - (view) Author: Dariusz Suchojad (dsuch) Date: 2010-10-25 09:30
I find that _AttributeHolder is a handy way for passing the command line options around the application. What is lacks though is a documented API for actually fetching the attributes in batches, like .items() or something similar that could be used for iterating over all command line arguments. That's why I thought '_get_kwargs' would be a good candidate particularly because it does exactly what I need in my code, returns a sorted list of key/value parameters.

But I'm not really saying that it must be '_get_kwargs', could as well be _AttributeHolder's __dict__ attribute as long as the docs say that it's a part of the public API so that I'm sure I'm not using something that may silently break between releases.
msg119544 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2010-10-25 09:34
Removing docs unless this actually becomes a doc issue
msg222394 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2014-07-06 05:56
Apart from sorting, `_get_kwargs` does little more than:

    return [k for k in action.__dict__.items() if not k[0].startswith('_')]

That is, it's the `items()` of the 'public' attributes of the action (or parser).  Those attributes are already accessible to the code that is using 'argparse'.
msg350854 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-08-30 06:15
Marking this as out-of-date.  It seems that the desired functionality has already been added in types.SimpleNamespace:

    # Capabilities of _AttributeHolder
    >>> ah = _AttributeHolder()
    >>> ah
    AttributeHolder()
    >>> ah.raymond='Red'
    >>> ah
    AttributeHolder(raymond='Red')
    >>> ah.raymond
    'Red'
    >>> ah.rachel='blue'
    >>> ah
    AttributeHolder(rachel='blue', raymond='Red')
    >>> ah._get_kwargs()
    [('rachel', 'blue'), ('raymond', 'Red')]

    # Capabilities of SimpleNamespace
    >>> import types
    >>> ah = types.SimpleNamespace()
    >>> ah.raymond='Red'
    >>> ah
    namespace(raymond='Red')
    >>> ah.rachel='blue'
    >>> ah
    namespace(rachel='blue', raymond='Red')
    >>> vars(ah).items()
    dict_items([('raymond', 'Red'), ('rachel', 'blue')])
History
Date User Action Args
2022-04-11 14:57:07adminsetgithub: 54399
2019-08-30 06:15:40rhettingersetstatus: open -> closed
resolution: out of date
messages: + msg350854

stage: resolved
2019-08-30 03:32:18rhettingersetassignee: bethard -> rhettinger

nosy: + rhettinger
2014-07-06 05:56:56paul.j3setmessages: + msg222394
2014-07-05 23:46:03BreamoreBoysetnosy: + paul.j3
2010-10-25 12:34:47eric.araujosetnosy: + eric.araujo

components: - Documentation
versions: + Python 3.2
2010-10-25 09:34:57tim.goldensetassignee: docs@python -> bethard
2010-10-25 09:34:51tim.goldensetnosy: + tim.golden, - docs@python
messages: + msg119544
2010-10-25 09:30:14dsuchsetmessages: + msg119543
2010-10-25 07:34:37bethardsetmessages: + msg119542
2010-10-25 04:44:45dsuchcreate