[issue42650] Can people use dest=argparse.SUPPRESS in custom Action classes?
New submission from Brad Warren <bradmwarren@gmail.com>: 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. ---------- assignee: docs@python components: Documentation messages: 383089 nosy: bmw, docs@python priority: normal severity: normal status: open title: Can people use dest=argparse.SUPPRESS in custom Action classes? versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue42650> _______________________________________
Change by hai shi <shihai1991@126.com>: ---------- nosy: +paul.j3, rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue42650> _______________________________________
paul j3 <ajipanca@gmail.com> added the comment: 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). ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue42650> _______________________________________
Brad Warren <bradmwarren@gmail.com> added the comment: 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/711381dfb09fbd434cc3b404656f7fd306161.... 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! ---------- stage: -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue42650> _______________________________________
participants (3)
-
Brad Warren -
hai shi -
paul j3