Skip to content

✨ Add option to use Enum names instead of values on the commandline#224

Open
sirex wants to merge 54 commits intofastapi:masterfrom
sirex:enum-names
Open

✨ Add option to use Enum names instead of values on the commandline#224
sirex wants to merge 54 commits intofastapi:masterfrom
sirex:enum-names

Conversation

@sirex
Copy link

@sirex sirex commented Jan 21, 2021

Fixes: #151

Added names parameter, in order to user Enum names instead of values. Also for IntEnum, names are used by default, even if names is False.

sirex added 3 commits January 21, 2021 18:57
Fixes: fastapi#151

Added `names` parameter, in order to user Enum names instead of values. Also for IntEnum, names are used by default, even if names is False.
@codecov

This comment was marked as outdated.

@Aerilius
Copy link

Thanks! This feature is so much missing when using enums with values of other types than strings.

However I got an error with this code:

    transform: SymmetryTransformation = typer.Option(SymmetryTransformation.TRANSPOSE, names=True),
Error: Invalid value for '--transform': invalid choice: SymmetryTransformation.TRANSPOSE. (choose from IDENTITY, ROT90, ROT180, ROT270, FLIP_X, FLIP_Y, TRANSPOSE, ANTITRANSPOSE)

I had to change the default to the value that should be provided on the CLI, not as received in the application:

    transform: SymmetryTransformation = typer.Option("TRANSPOSE", names=True),

@mattchan-tencent
Copy link

Is there some update on whether this can be merged? This would make the Choices feature much more pythonic.

@abondrnco
Copy link

Bumping this for interest, surprised this isn't the default behavior but understandable if all of the exploration done around this feature was with string enums

Copy link
Contributor

@ryangalamb ryangalamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a super useful feature, but I noticed some issues while reviewing this:

  • The default values are broken. Should add a test case to cover that too. (Also pointed out by @Aerilius)
  • The name names should be changed to indicate that it's specific to enums.
  • The implicit behavior with IntEnum will likely cause confusion. I'd prefer consistency.

I'd also like to see a test case covering Argument. (I see that this is implemented for Argument, but a test case would help guard against accidentally breaking it.)

I know this has been open for a while, so no worries if you don't have the time/interest to keep working on this. Either way, thank you for getting this started!

@svlandeg svlandeg added feature New feature, enhancement or request p3 labels Mar 6, 2024
@svlandeg
Copy link
Member

Hi, thanks for the PR and apologies for the delay in reviewing this!

I'll put this in draft and I'll update with the latest master, and then I'll review in more detail.

@svlandeg svlandeg self-assigned this Aug 14, 2024
@svlandeg svlandeg marked this pull request as draft August 14, 2024 13:05
@github-actions github-actions bot added the docs Improvements or additions to documentation label Aug 14, 2024
@svlandeg svlandeg changed the title Add option to use Enum names ✨ Add option to use Enum names Aug 26, 2024
@github-actions

This comment was marked as outdated.

@svlandeg svlandeg removed the docs Improvements or additions to documentation label Sep 11, 2024
@svlandeg svlandeg marked this pull request as draft October 7, 2025 14:39
@svlandeg svlandeg marked this pull request as ready for review October 7, 2025 15:02
@svlandeg svlandeg removed their assignment Oct 7, 2025
@svlandeg svlandeg self-assigned this Nov 25, 2025
@svlandeg svlandeg marked this pull request as draft November 25, 2025 16:54
@svlandeg svlandeg removed their assignment Nov 25, 2025
@svlandeg svlandeg marked this pull request as ready for review November 25, 2025 17:15
@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Dec 25, 2025
@github-actions

This comment was marked as resolved.

@svlandeg svlandeg self-assigned this Jan 7, 2026
@svlandeg svlandeg marked this pull request as draft January 7, 2026 14:22
@github-actions github-actions bot removed the conflicts Automatically generated when a PR has a merge conflict label Jan 13, 2026
Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had another look at this PR, and brought it up-to-date with the latest docs formatting on master. I've also removed several unnecessary test files that weren't actually mentioned in the tutorial.

I think the PR is now again ready for review 🙏

@svlandeg svlandeg marked this pull request as ready for review January 28, 2026 15:47
@svlandeg svlandeg removed their assignment Jan 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature, enhancement or request p3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Use enum.name as parameter, and enum.value as value received by the function

6 participants