Skip to content

Strange behavior argparse.BooleanOptionalAction #133638

@tomazfs

Description

@tomazfs

Bug report

Bug description:

In [1]: import argparse
   ...: parser = argparse.ArgumentParser()
   ...: parser.add_argument('--foobar', action=argparse.BooleanOptionalAction)
   ...: parser.add_argument('--foo', action="store_true")
   ...: parser.parse_args(['--no-foo'])
Out[1]: Namespace(foobar=False, foo=False)

In [2]: parser = argparse.ArgumentParser()
   ...: parser.add_argument('--foo-bar', action=argparse.BooleanOptionalAction)
   ...: parser.add_argument('--foo', action="store_true")
   ...: parser.parse_args(['--no-foo'])
Out[2]: Namespace(foo_bar=False, foo=False)

Is this expected? There is no '--no-foo' option, but using it sets '--foobar', '--foo-bar' and '--foo' to False. I have tested this on Python 3.12.10 and Python 3.13.3.

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Doc issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions