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 people use dest=argparse.SUPPRESS in custom Action classes?
Type: Stage: resolved
Components: Documentation Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: bmw, docs@python, paul.j3, rhettinger
Priority: normal Keywords:

Created on 2020-12-15 18:49 by bmw, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg383089 - (view) Author: Brad Warren (bmw) Date: 2020-12-15 18:49
argparse internally sets dest=SUPPRESS in action classes like _HelpAction and _VersionAction to prevent an attribute from being created for that option on the resulting namespace.

Can users creating custom Action classes also use this functionality without worrying about it suddenly breaking in a new version of Python? If so, can we document this functionality? I'd be happy to submit a PR.
msg383442 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2020-12-20 19:28
I'd have to study the code (and docs), but I'm not sure setting the `dest` to 'SUPPRESS' does anything meaningful.

    default=argparse.SUPPRESS

is useful, keeping the default out of the namespace.  That argument appears only if the user has used the option.

But with `dest` (in a default 'store'), I get a namespace like

    Namespace(**{'==SUPPRESS==': 'foo'})

'help' and 'version' exit right after displaying their message, so I'm no sure the 'SUPPRESS' is doing anything.  The parser doesn't return a namespace.

It appears that the 'dest' is passed to the Action.  A custom Action could act on that 'dest'.  The default 'store' just uses it as the attribute for storing the value in the namespace.

Anyways, this is not the kind of thing that we'll be tweaking in the source.  I don't recall any bug/issue touching on this behavior (but we could do a search).
msg383546 - (view) Author: Brad Warren (bmw) Date: 2020-12-21 18:09
Thanks for the help! I was mistaken here.

The behavior I want is to not add the option to the namespace regardless of whether or not the user set it. I was initially under the impression that dest=SUPPRESS caused this behavior, however, it seems to have the same behavior as default=SUPPRESS due to the code at https://github.com/python/cpython/blob/711381dfb09fbd434cc3b404656f7fd306161a64/Lib/argparse.py#L1838-L1841. By using default=SUPPRESS which is documented and a custom __call__ method on an Action class, I can get the behavior I want so I'm closing this issue.

Thanks again and sorry for the noise!
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86816
2020-12-21 18:09:21bmwsetstatus: open -> closed

messages: + msg383546
stage: resolved
2020-12-20 19:28:57paul.j3setmessages: + msg383442
2020-12-20 15:15:32shihai1991setnosy: + rhettinger, paul.j3
2020-12-15 18:49:25bmwcreate