Skip to content

Inconsistent validation of defaults in argparse #100949

Closed as not planned
Closed as not planned
@ton

Description

@ton

Bug report

The following does not trigger an exception in ArgumentParser, where I would expect an exception since the default value for --choice is not in the list of choices:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--choice', choices=['a', 'b', 'c'], default='foo')

args = parser.parse_args()                    # args.choice == 'foo'

This is at least inconsistent behaviour, the following piece of code does trigger an exception for an invalid default value:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--num', type=int, default='foo')

args = parser.parse_args(args=[])

Environment: Python 3.10.9, Arch Linux, x86_64.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Doc issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions