-
-
Notifications
You must be signed in to change notification settings - Fork 826
✨ Add option to use Enum names instead of values on the commandline #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
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.
This comment was marked as outdated.
This comment was marked as outdated.
|
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),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), |
|
Is there some update on whether this can be merged? This would make the Choices feature much more pythonic. |
|
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
left a comment
There was a problem hiding this 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
namesshould be changed to indicate that it's specific to enums. - The implicit behavior with
IntEnumwill 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!
|
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 |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
📝 Docs previewLast commit d516837 at: https://ae333746.typertiangolo.pages.dev Modified Pages |
| 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] *} |
There was a problem hiding this comment.
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.
| @@ -0,0 +1,25 @@ | |||
| from enum import Enum | |||
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
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.
Fixes: #151
Added
namesparameter, in order to user Enum names instead of values. Also for IntEnum, names are used by default, even if names is False.