Skip to content

Choice prefers Enum.name even for StrEnum where it should use the value #2911

@jwodder

Description

@jwodder

Consider the following code:

from enum import Enum
import click


class Color(str, Enum):
    RED = "red"
    GREEN = "green"
    BLUE = "blue"

    def __str__(self) -> str:
        return self.value


@click.command()
@click.option("-C", "--color", type=click.Choice(list(Color)), show_default=True)
def main(color: Color) -> None:
    print(repr(color))


if __name__ == "__main__":
    main()

With Click 8.1.8, running this script with -C red works as expected, but with Click 8.2.0, it instead fails with:

Usage: enum-choice01a.py [OPTIONS]
Try 'enum-choice01a.py --help' for help.

Error: Invalid value for '-C' / '--color': 'red' is not one of 'RED', 'GREEN', 'BLUE'.

This was observed with Python 3.13.3 on macOS 14.7.5.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions