Skip to main content

LiteralEnum

LiteralEnum is an experiment/prototype for a proposed Python typing construct: a finite, named set of runtime literals (usually strings) that type checkers can treat as an exhaustive Literal[...] union.

Status: Prototype / exploration for typing-sig discussion. Not an accepted PEP.


Why this exists

In typed Python today you often have to pick one:

  • Enum / StrEnum: great runtime namespace, but APIs want callers to pass enum members instead of raw strings
  • Literal[...]: great static checking, but no runtime namespace/iteration/validation

So people duplicate values or accept str and validate at runtime.

LiteralEnum aims to make the common case a single source of truth.


What is it?

  1. ✅ A runtime python package provided with pip install literalenum
  2. ✅ A mypy plugin for correctly type checking the new construct in a way that matches the runtime behavior
  3. ✅ A Pycharm Plugin for correct syntax highlighting to match the runtime behavior
  4. ✅ A discussion in hopes of creating support for a PEP to help solve these painpoints in the Python language, such that type checker plugins are not needed
  5. ⚠️ TODO: a basedpyright fork VS Code plugin to provide pylance-esque syntax highlighting in VS Code

Table of Contents


Quickstart

Install

This repo is currently set up as a package under src/.

#python -m venv .venv
#source .venv/bin/activate
pip install literalenum

Usage

The following is valid at runtime, but will not pass static type-checking correctly unless you use a provided plugin.

from literalenum import LiteralEnum

class HttpMethod(LiteralEnum):
    GET = "GET"
    POST = "POST"
    DELETE = "DELETE"

def handle(method: HttpMethod) -> None:
    print(f"{method=}")

handle("GET")          # the GOAL is that this should type-check ✅ , in reality: it will not unless typecheckers change
handle(HttpMethod.GET) # the GOAL is that this should type-check ✅ , in reality: it will not unless typecheckers change
handle("git")          # ❌ should be rejected by a type checker

assert HttpMethod.GET == "GET"
assert list(HttpMethod) == ["GET", "POST", "DELETE"]
assert "GET" in HttpMethod
print(HttpMethod.keys())
print(HttpMethod.values())
print(HttpMethod.mapping)

Pydantic v2

LiteralEnum includes .literal() and .base_model() converters. It also supports direct Pydantic v2 annotations, including TypeAdapter, without a separate adapter:

from literalenum import LiteralEnum
from pydantic import BaseModel


class HttpMethod(LiteralEnum):
    GET = "GET"
    POST = "POST"


class Request(BaseModel):
    method: HttpMethod


if __name__ == "__main__":
    example_request = Request(method=HttpMethod.GET)
    example_json = example_request.model_dump_json()
    example_schema = Request.model_json_schema()
    print(example_json, example_schema)

Values remain plain scalars. Validation rejects unknown members and type coercions (including True or 1.0 for an integer 1 member); JSON schema uses the same namespace. Keep the LiteralEnum mypy plugin enabled for static literal narrowing. Pydantic stays an optional integration and is imported only when used.

Contributing / discussion

Actively looking for feedback! Please comment at https://discuss.python.org/t/proposal-literalenum-runtime-literals-with-static-exhaustiveness/106000

It would be especially helpful if you are familiar with mypy/pright/pylance and have suggestions on how a future Python version could support the type hinting goals.


License

This project is released into the public domain under The Unlicense.

You are free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

See the LICENSE file for full details.

Metadata

Release files for literalenum 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for literalenum 0.5.0
File Size Uploaded Provenance Actions
literalenum-0.5.0.tar.gz 330.8 kB With provenance Details for literalenum-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for literalenum 0.5.0
File Interpreter ABI Platform Actions
literalenum-0.5.0-py3-none-any.whl Python 3 none any Details for literalenum-0.5.0-py3-none-any.whl

Total release size: 360.8 kB

Release files / literalenum-0.5.0.tar.gz

Download URL literalenum-0.5.0.tar.gz
Size 330.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6e7d09f286bb2d9b615f00f7ea5786dc3fdff45c67204add7a719b80efeb99c3
BLAKE2b-256 checksum
How to use checksums
27e154504c71446e7c198f5287bd1d820b56f2268f240ed580b37a1748b702ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Release files / literalenum-0.5.0-py3-none-any.whl

Download URL literalenum-0.5.0-py3-none-any.whl
Size 30.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
069f4088b1e1b95330124d6c7f7447471256fc0d895e79db00f1472b8208f3c9
BLAKE2b-256 checksum
How to use checksums
e088ae9f42c177384f971668773a8d18203c80852e499a05960fefb1851ca1a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Security for literalenum 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for literalenum-0.5.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page