Skip to content

📝 Add documentation for enums created via the functional API#398

Open
Torvaney wants to merge 20 commits intofastapi:masterfrom
Torvaney:enum-functional-api-docs
Open

📝 Add documentation for enums created via the functional API#398
Torvaney wants to merge 20 commits intofastapi:masterfrom
Torvaney:enum-functional-api-docs

Conversation

@Torvaney
Copy link
Copy Markdown

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

@github-actions

This comment was marked as outdated.

@codecov

This comment was marked as outdated.

Copy link
Copy Markdown

@joaonc joaonc left a comment

Choose a reason for hiding this comment

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

Looks great! Hope it gets merged soon.

@svlandeg svlandeg added the docs Improvements or additions to documentation label Jun 13, 2022
@github-actions

This comment was marked as outdated.

@svlandeg svlandeg added the p3 label Mar 6, 2024
@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

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

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.

@svlandeg svlandeg changed the title Add documentation for enums created via the functional API 📝 Add documentation for enums created via the functional API Jul 19, 2024
@github-actions

This comment was marked as outdated.

@svlandeg svlandeg marked this pull request as draft August 28, 2025 10:05
@svlandeg svlandeg self-assigned this Aug 28, 2025
@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@svlandeg svlandeg marked this pull request as ready for review August 28, 2025 13:18
@svlandeg svlandeg removed their assignment Aug 28, 2025
@github-actions

This comment was marked as outdated.

import typer
from typing_extensions import Annotated

NeuralNetwork = Enum("NeuralNetwork", {k: k for k in ["simple", "conv", "lstm"]})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

mypy gives:

main.py:10: error: Incompatible default for argument "network" (default has type "str", argument has type "NeuralNetwork")  [assignment]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

IIRC, this only works if the Enum extends str. You can either use StrEnum or have an something like class MyEnum(str, Enum)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 mypy complains (we don't test for it on the CI anyway, but good catch @YuriiMotov!)
  • merge without the Annotated example
  • don't merge and assume there are enough examples to use Enum and users will figure out what's best for them

Copy link
Copy Markdown
Member

@YuriiMotov YuriiMotov Nov 4, 2025

Choose a reason for hiding this comment

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

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

@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 Sep 22, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Nov 3, 2025

@svlandeg svlandeg removed their assignment Nov 4, 2025
@svlandeg svlandeg self-assigned this Nov 25, 2025
@svlandeg svlandeg marked this pull request as draft November 25, 2025 16:07
@svlandeg svlandeg marked this pull request as ready for review November 25, 2025 16:38
@svlandeg svlandeg removed their assignment Nov 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation p3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Must use typer.Option to supply default argument with Enums created with functional API

5 participants