Skip to content

Argparse: Unable to combine choices with type #132558

Open
@hansthen

Description

@hansthen

Bug report

Bug description:

It is impossible to combine the argparse type and choices arguments in a meaningful way.

import argparse

parser = argparse.ArgumentParser()
days = ["mo", "tu", "we", "th", "fr", "sa", "su"]
def name_day(value):
    return days.index(value) + 1

parser.add_argument(
    '--days',
    type=name_day,
    choices=days,
)
args = [
    "--days", "mo", "tu"
]
result = parser.parse_args(args)

This will result in the following error message:

example.py: error: argument --days: invalid choice: 1 (choose from 'mo', 'tu', 'we', 'th', 'fr', 'sa', 'su')

CPython versions tested on:

3.10, 3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions