Skip to main content

A Python typing construct that provides namespaced literal constants with advanced typing features.

Project description

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.

It’s designed for “protocol token” style values—HTTP methods, event names, command identifiers, config keys—where you want:

  • plain literals at runtime (e.g. "GET"),
  • namespaced constants (e.g. HttpMethod.GET), and
  • static exhaustiveness checking (i.e. the type is equivalent to Literal["GET", "POST", ...]).

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
from literalenum import LiteralEnum

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

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

handle("GET")          # ✅ should type-check
handle(HttpMethod.GET) # ✅ should type-check
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)

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

literalenum-0.1.1.tar.gz (41.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

literalenum-0.1.1-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file literalenum-0.1.1.tar.gz.

File metadata

  • Download URL: literalenum-0.1.1.tar.gz
  • Upload date:
  • Size: 41.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for literalenum-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2ba20a011ae261cc99c5513678faed34fe3cddd5afbdc3c5bfdc1fce56006da8
MD5 43d163432b0bbaee19983cc09eb732c2
BLAKE2b-256 811382485c72c14911170686064cb714cc1f630eb34f037ed230cc6cae0f038e

See more details on using hashes here.

Provenance

The following attestation bundles were made for literalenum-0.1.1.tar.gz:

Publisher: publish.yml on modularizer/LiteralEnum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file literalenum-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: literalenum-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for literalenum-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 003b5fdb79059f21f6cddb2266c686fddbf44412899055cf869024920bfc9db7
MD5 e3f80d2fed42d84588701d9924950af6
BLAKE2b-256 4576da3d1c24fd29a7a90fc930a26cee8ce53bc3133ea929561939ba461f180c

See more details on using hashes here.

Provenance

The following attestation bundles were made for literalenum-0.1.1-py3-none-any.whl:

Publisher: publish.yml on modularizer/LiteralEnum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page