Skip to content

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 removed their assignment Aug 28, 2025
@github-actions

This comment was marked as outdated.

@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Sep 20, 2025
@github-actions

This comment was marked as resolved.

@svlandeg svlandeg self-assigned this Sep 20, 2025
@github-actions github-actions bot removed the conflicts Automatically generated when a PR has a merge conflict label Oct 7, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Oct 7, 2025

that into `Enum` values in the command handler. You can enable this by setting
`enum_by_name=True`:

{* docs_src/parameter_types/enum/tutorial007_an.py hl[14] *}
Copy link
Member

Choose a reason for hiding this comment

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

Note: throughout this file, the numbering of the tutorials is now pretty disorganized, because of the addition of new ones in between existing ones. I suggest to keep this as-is in this PR to not complicate the git history, but if we're annoyed by the out-of-sequence usage of tutorials here (001-002-007-005-003-006-004), we could fix it afterwards.

@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
@@ -0,0 +1,25 @@
from enum import Enum
Copy link
Member

Choose a reason for hiding this comment

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

A quick note that this tutorial file isn't actually referenced from anywhere within the tutorial markdown files.

@@ -0,0 +1,29 @@
from enum import Enum
Copy link
Member

Choose a reason for hiding this comment

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

A quick note that this tutorial file isn't actually referenced from anywhere within the tutorial markdown files.

@@ -0,0 +1,29 @@
from enum import Enum
Copy link
Member

Choose a reason for hiding this comment

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

A quick note that this tutorial file isn't actually referenced from anywhere within the tutorial markdown files.

@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
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