Skip to content

Conversation

@blackary
Copy link
Contributor

Copied PR #174 from someone who deleted their account because I think it's worth reopening this PR and adding this functionality to typer.

Original PR message:

Support for typing.Literal for creating choices, without actually having to create Enum types. Requires Python 3.8, referenced in #76. I ran tests again 3.6 without problems, so I think the version check if working OK.

@github-actions

This comment was marked as outdated.

@codecov

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@prompteus
Copy link

Any updates on merging this? @tiangolo

@hoishing
Copy link

hoishing commented Feb 4, 2023

Upvote for this PR, as using Literal is more succinct then using enum.

Copy link

@atmartinez atmartinez left a comment

Choose a reason for hiding this comment

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

There is the possibility to import Literal from typing_extensions to allow this to work for 3.6 (since Typer still says it support it despite being EOL) and 3.7 as well. This may be a moot consideration soon though since 3.7 goes EOL in June 2023 as well if there are any plans to drop unsupported Python versions at some point.

This may require adding a specific dependency for typing_extensions, but it's currently being installed by another dep regardless from some of the CI runs I checked for 3.6+ 4.1-4.4 depending on the Python version.

Example implementation for using typing_extensions

@svlandeg svlandeg added feature New feature, enhancement or request types Type hints and type checking p3 labels Feb 29, 2024
@svlandeg svlandeg marked this pull request as draft June 3, 2024 12:45
@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@svlandeg svlandeg marked this pull request as draft August 27, 2025 13:49
@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@svlandeg
Copy link
Member

Update:

  • Removed the needs_py38 as per Kludex's comment
  • Updated the tests that started failing due to other changes on the master branch after the CI ran last time on this PR. All green again.

@svlandeg svlandeg marked this pull request as ready for review August 27, 2025 14:19
@svlandeg svlandeg removed their assignment Aug 27, 2025
@svlandeg svlandeg changed the title ✨ Support typing.Literal in Python 3.7+ ✨ Support typing.Literal to define a set of predefined choices Aug 28, 2025
@svlandeg svlandeg self-assigned this Aug 28, 2025
@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@svlandeg svlandeg removed their assignment Aug 28, 2025
@github-actions

This comment was marked as outdated.

@pdjohntony
Copy link

I was really hoping this got merged for the latest release 2 weeks ago 👀

Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

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

Amazing, thank you @blackary! 🚀

And thank you everyone for the discussions and help. ☕

Thanks @svlandeg and @Kludex!


This will be (finally) available in Typer 0.19.0, released in the next few hours. 🎉

@github-actions

This comment was marked as outdated.

@tiangolo tiangolo changed the title ✨ Support typing.Literal to define a set of predefined choices ✨ Support typing.Literal to define a set of predefined choices Sep 20, 2025
@github-actions
Copy link
Contributor

@tiangolo tiangolo merged commit 6774f1d into fastapi:master Sep 20, 2025
25 checks passed
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 types Type hints and type checking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Literal type support