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.

Author zachrahan
Recipients zachrahan
Date 2017-01-17.14:52:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1484664769.91.0.625725054847.issue29298@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2017-01-17 14:52:49zachrahansetrecipients: + zachrahan
2017-01-17 14:52:49zachrahansetmessageid: <1484664769.91.0.625725054847.issue29298@psf.upfronthosting.co.za>
2017-01-17 14:52:49zachrahanlinkissue29298 messages
2017-01-17 14:52:49zachrahancreate