📝 Add documentation for enums created via the functional API#398
📝 Add documentation for enums created via the functional API#398Torvaney wants to merge 20 commits intofastapi:masterfrom
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
joaonc
left a comment
There was a problem hiding this comment.
Looks great! Hope it gets merged soon.
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 outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
svlandeg
left a comment
There was a problem hiding this comment.
Hi! Thanks for this contribution, and apologies for the delay in reviewing.
I agree that this additional section in the documentation could make it more clear for users on how to use "pure" Python Enum's with Typer.
I updated the PR with the latest from master and also included an Annotated version.
It's probably good if Tiangolo has another look at the exact phrasing in the documentation. I'll leave the final decision to merge or not with him.
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 outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
| import typer | ||
| from typing_extensions import Annotated | ||
|
|
||
| NeuralNetwork = Enum("NeuralNetwork", {k: k for k in ["simple", "conv", "lstm"]}) |
There was a problem hiding this comment.
mypy gives me
main.py:6: error: Second argument of Enum() must be string, tuple, list or dict literal for mypy to determine Enum members [misc]
There was a problem hiding this comment.
This particular error is a bit of a mypy quirck, if you have it as
NeuralNetwork = Enum("NeuralNetwork", {"simple": "simple", "conv": "conv", "lstm": "lstm"})
then it does pass the type check.
|
|
||
|
|
||
| def main( | ||
| network: Annotated[NeuralNetwork, typer.Option(case_sensitive=False)] = "simple", |
There was a problem hiding this comment.
mypy gives:
main.py:10: error: Incompatible default for argument "network" (default has type "str", argument has type "NeuralNetwork") [assignment]
There was a problem hiding this comment.
IIRC, this only works if the Enum extends str. You can either use StrEnum or have an something like class MyEnum(str, Enum)
There was a problem hiding this comment.
Ah, that's unfortunate. We already have examples in the tutorial using Food(str, Enum), no need to repeat that here. And the current example does work, even if mypy complains. So I'm a bit on the fence what to do here. I guess our options are:
- merge as-is even if
mypycomplains (we don't test for it on the CI anyway, but good catch @YuriiMotov!) - merge without the
Annotatedexample - don't merge and assume there are enough examples to use
Enumand users will figure out what's best for them
There was a problem hiding this comment.
I think it's better to "merge without the Annotated example".
Alternative would be to add # type: ignore[assignment] and explain that for now we have to ignore this error with Annotated approach
This comment was marked as resolved.
This comment was marked as resolved.
📝 Docs previewLast commit 556376a at: https://4436b468.typertiangolo.pages.dev Modified Pages |
This PR updates the documentation to include discussion of enumerating choices with
enum.Enums created with the functional API. This is useful when creating choices dynamically, for example logging.See further discussion of enums and the functional API in #389