New submission from Toshio Kuratomi <a.bad...@gmail.com>:

This is related to https://bugs.python.org/issue38956 but a different symptom 
(and the current proposed fix for 38956 will not fix this.  My proposed fixes 
for this would also fix 38956).

I have the following code which uses BooleanOptionalAction along with a default 
of SUPPRESS (I use SUPPRESS because I merge these with settings from config and 
environment variables and then validate and add default values using a schema.  
SUPPRESS allows me to tell when a value was not specified at the command line 
by the user):

    whole_site_parser.add_argument('--indexes',    
                                   dest='indexes', 
action=BooleanOptionalAction,    
                                   default=argparse.SUPPRESS,    
                                   help='Test')


That code outputs:

  --indexes, --no-indexes
                        Test (default: ==SUPPRESS==)

Similar code that does not use BooleanOptionalAction does not show default: 
==SUPPRESS == even when formatter_class=ArgumentDefaultsHelpFormatter is used.

Looking at the code, this is occurring because BooleanOptionalArgument has its 
own code to add default: on its own (instead of leaving formatting as the 
responsibility of the formatter_class). The code in BooleanOptionalArgument 
handles less cases where the default should be skipped than the 
ArgumentDefaultsHelpFormatter code; SUPPRESS is one of the missing cases.

I can see two ways of fixing this:

(1) Remove the code from BooleanOptionalArgument that adds the default values.  
It seems to violate the architecture of argparse which delegates modifications 
to the help message to the formatter_class so this is a special case which 
could cause issues for future modifications as well.

(2) If the usefulness of outputting the default values without changing the 
formatter_class is deemed too important to relinquish, then moving the code 
that ArgumentDefaultsHelpFormatter uses to determine when to skip adding 
default to the help message can be extracted from ArgumentDefaultsHelpFormatter 
and called by both ArgumentDefaultsHelpFormatter and BooleanOptionalArgument .

In a review of a fix for 
https://github.com/python/cpython/pull/17447/files#r429630944 raymond hettinger 
thought that outputting of the default values was important to keep although 
I'm not clear on whether he considered that the usefulness comes at the price 
of possibly violating argparse's architecture.  If he hasn't changed his mind, 
then #2 is probably the way to resolve this.

I can submit a PR for either of these once I know which direction to take (the 
first is just removing a few lines of code and I've already written the second 
one since it seemed like the direction that raymond had last asked for).

Please let me know how you'd like me to proceed.

----------
messages: 397184
nosy: a.badger
priority: normal
severity: normal
status: open
title: BooleanOptionalAction displays default=SUPPRESS unlike other action types
versions: Python 3.10, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44587>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to