python/cpython · error · ValueError

nargs for store actions must be != 0; if you have nothing to

Error message

nargs for store actions must be != 0; if you have nothing to store, actions such as store true or store const may be more appropriate

What it means

_StoreAction (the default action for add_argument with a value) rejects nargs=0 at construction: an argument that stores nothing is contradictory, and argparse points you at store_true/store_const which are designed for zero-argument flags. The check fires when the parser is built, before any parsing.

Source

Thrown at Lib/argparse.py:1111

        return ' | '.join(self.option_strings)


class _StoreAction(Action):

    def __init__(self,
                 option_strings,
                 dest,
                 nargs=None,
                 const=None,
                 default=None,
                 type=None,
                 choices=None,
                 required=False,
                 help=None,
                 metavar=None,
                 deprecated=False):
        if nargs == 0:
            raise ValueError('nargs for store actions must be != 0; if you '
                             'have nothing to store, actions such as store '
                             'true or store const may be more appropriate')
        if const is not None and nargs != OPTIONAL:
            raise ValueError('nargs must be %r to supply const' % OPTIONAL)
        super(_StoreAction, self).__init__(
            option_strings=option_strings,
            dest=dest,
            nargs=nargs,
            const=const,
            default=default,
            type=type,
            choices=choices,
            required=required,
            help=help,
            metavar=metavar,
            deprecated=deprecated)

    def __call__(self, parser, namespace, values, option_string=None):

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. For a flag with no value, use action='store_true' (or 'store_false', 'store_const').
  2. If the argument takes exactly one value, omit nargs entirely.
  3. Guard generated nargs values: skip or default to 1 when the computed count is 0.

Example fix

# before
parser.add_argument('--verbose', nargs=0)  # ValueError: nargs for store actions must be != 0

# after
parser.add_argument('--verbose', action='store_true')
Defensive patterns

Strategy: validation

Validate before calling

def normalize_nargs(nargs):
    if nargs == 0:
        raise ValueError('nargs=0 is invalid for store actions; use store_true')
    return nargs

Prevention

When it happens

Trigger: parser.add_argument('--flag', nargs=0); programmatically passing a computed nargs that evaluates to 0; copying nargs from another argument without adjusting it.

Common situations: Generated CLIs where a count value of 0 becomes nargs=0; confusion between nargs and type (nargs=0 is never valid for storing); refactors that move a flag from store_true to the default action.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/cdfdfaca9e16351c. Report an issue: GitHub.