Message285646
In python 3.6 (and several versions back), using argparse with required subparsers will cause an unhelpful TypeError if the 'dest' parameter is not explicitly specified, and no arguments are provided.
Test case:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparsers.required = True
args = parser.parse_args([])
Observed result:
TypeError: sequence item 0: expected str instance, NoneType found
If the line above is changed to:
subparsers = parser.add_subparsers(dest='function')
Then the following is printed to stderr:
usage: python [-h] {} ...
python: error: the following arguments are required: function
This issue goes back at least several years:
http://stackoverflow.com/questions/23349349/argparse-with-required-subparser/23354355
Though it seems odd to not specify a dest in the add_subparsers line, the pattern is not completely useless. The below works fine without setting a 'dest' in add_subparsers, except when argv is empty:
sub1 = subparsers.add_parser('print')
sub1.set_defaults(function=print)
However, an empty argv produces the unexpected TypeError above. I'm not sure if argparse should provide a more useful exception in this case, or if there is a clean way to do the right thing without a dest specified. |
|
Date |
User |
Action |
Args |
2017-01-17 14:52:49 | zachrahan | set | recipients:
+ zachrahan |
2017-01-17 14:52:49 | zachrahan | set | messageid: <1484664769.91.0.625725054847.issue29298@psf.upfronthosting.co.za> |
2017-01-17 14:52:49 | zachrahan | link | issue29298 messages |
2017-01-17 14:52:49 | zachrahan | create | |
|