[New-bugs-announce] [issue43874] argparse crashes on subparsers with no dest/metava

Terence Honles report at bugs.python.org
Fri Apr 16 15:07:46 EDT 2021

New submission from Terence Honles <terence.honles at gmail.com>:

When creating a simple required subparser the following will result in an error instead of an error message:

>>> from argparse import ArgumentParser
>>> parser = ArgumentParser()
>>> subparsers = parser.add_subparsers(required=True)
>>> subparsers.add_parser('one')
>>> subparsers.add_parser('two')
>>> parser.parse_args([])

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.8/argparse.py", line 1768, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/usr/lib64/python3.8/argparse.py", line 1800, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/usr/lib64/python3.8/argparse.py", line 2035, in _parse_known_args
    ', '.join(required_actions))
TypeError: sequence item 0: expected str instance, NoneType found

Looking at the code this is because the subparser is actually an action and when resolving the name it uses `None` as the subparser name which results in the error above.

components: Library (Lib)
messages: 391250
nosy: terence.honles
priority: normal
pull_requests: 24176
severity: normal
status: open
title: argparse crashes on subparsers with no dest/metava
type: crash
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list