Skip to content

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

Closed
sirex wants to merge 64 commits into
fastapi:masterfrom
sirex:enum-names
Closed

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

Conversation

@sirex

@sirex sirex commented Jan 21, 2021

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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

@abondrnco

Copy link
Copy Markdown

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

@ryangalamb ryangalamb left a comment

Copy link
Copy Markdown
Contributor

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!

Comment thread docs/tutorial/parameter-types/enum.md Outdated
Comment thread docs_src/parameter_types/enum/tutorial003.py Outdated
Comment thread docs/tutorial/parameter-types/enum.md Outdated
Comment thread docs_src/parameter_types/enum/tutorial004.py Outdated
@svlandeg svlandeg added feature New feature, enhancement or request p3 labels Mar 6, 2024
@svlandeg

Copy link
Copy Markdown
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 removed their assignment Jan 28, 2026
@github-actions github-actions Bot added the conflicts Automatically generated when a PR has a merge conflict label Feb 10, 2026
@github-actions

This comment was marked as resolved.

@svlandeg svlandeg self-assigned this Feb 11, 2026
@github-actions github-actions Bot removed the conflicts Automatically generated when a PR has a merge conflict label Feb 14, 2026
@svlandeg svlandeg removed their assignment Feb 14, 2026
@github-actions github-actions Bot added the conflicts Automatically generated when a PR has a merge conflict label Feb 16, 2026
@github-actions

This comment was marked as resolved.

@github-actions github-actions Bot removed the conflicts Automatically generated when a PR has a merge conflict label Feb 17, 2026
@svlandeg svlandeg marked this pull request as draft February 17, 2026 08:45
@svlandeg svlandeg self-assigned this Feb 17, 2026
@github-actions github-actions Bot added the conflicts Automatically generated when a PR has a merge conflict label Feb 17, 2026
@github-actions

This comment was marked as resolved.

@github-actions github-actions Bot removed the conflicts Automatically generated when a PR has a merge conflict label Feb 17, 2026
@svlandeg svlandeg removed their assignment Feb 17, 2026
@svlandeg svlandeg marked this pull request as ready for review February 17, 2026 11:31
@github-actions github-actions Bot added the conflicts Automatically generated when a PR has a merge conflict label May 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has a merge conflict that needs to be resolved.

@tiangolo

Copy link
Copy Markdown
Member

Thanks for the work and interest on this! There are several refactors we needed to do, and some more we are still missing before tackling this.

I didn't want to close PRs before providing an alternative solution, but I ended up leaving too many PRs open for too long, sorry for that.

For now I'll close this one for cleanup, but we'll continue tracking this feature request in the discussion and internally. Thanks! ☕

@tiangolo tiangolo closed this May 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicts Automatically generated when a PR has a merge conflict 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

7 participants