← All notes

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

Sources

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

Permanent ID: iil:note:swk565nc

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.},
}

Markdown

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.

All artifact files (JSON index)

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.