Restrict a type to a specific value with typing.Literal
Answer: Literal is a special typing form to define "literal types". Literal indicates a value equivalent to one of the provided literals. PEP 586 adds those types to the PEP 484 ecosystem.
A specific value
Literal types indicate that some expression has literally a specific value. The PEP's example is a function that will accept only expressions that have literally the value "4". Multiple literals can be listed in one Literal[...] form.
Writing the annotation
Use Literal[True] on a parameter or a return type.
| Literal | meaning | |---|---| | Literal | Special typing form for literal types | | specific value | The expression has literally that value | | provided literals | Value equivalent to one of the provided literals | | PEP 586 | Adds Literal types to the PEP 484 ecosystem |
See also
Claims
- id: C1 claim: PEP 586 adds Literal types to indicate an expression has a specific value. source: S1 evidence: "This PEP proposes adding Literal types to the PEP 484 ecosystem"
- id: C2 claim: Literal types indicate that some expression has literally a specific value. source: S1 evidence: "Literal types indicate that some expression has literally a specific value"
- id: C3 claim: typing.Literal tells type checkers the annotated object has one of the provided literals. source: S2 evidence: "the annotated object has a value equivalent to one of the provided literals"
Sources
- [S1] PEP 586 – Literal Types. Python PEPs. https://peps.python.org/pep-0586/ . License: Public-Domain. Accessed 2026-11-19.
- [S2] typing — Support for type hints. Python docs. https://docs.python.org/3/library/typing.html . License: PSF-2.0. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 586 – Literal Types; Python docs, typing — Support for type hints. Public-Domain; PSF-2.0. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 586 – Literal Types; Python docs, typing — Support for type hints. Public-Domain; PSF-2.0.
Cite this
iinventedlight. 2026. "Restrict a type to a specific value with typing.Literal." iinventedlight Library, iil:note:swk565nc, version of 2026-11-19. https://iinventedlight.com/library/notes/python-typing-literal/. License: CC-BY-4.0.
@misc{iil_note_swk565nc,
author = {{iinventedlight}},
title = {{Restrict a type to a specific value with typing.Literal}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/python-typing-literal/},
note = {Permanent ID iil:note:swk565nc. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-typing-literal.table.json (table; CC-BY-4.0)
- python-typing-literal.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-typing-literal.md.