A Python typing construct that provides namespaced literal constants with advanced typing features.
Project description
from sample_str_enum_solutions.a_strenum import HttpMethod
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 stringsLiteral[...]: 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
- Typing discussion with the Python community
- PEP.md is a draft PEP
- LITMUS.md describes the project goals
- TYPING_DISCUSSION.md is shows drafts from the discussion
- src/typing_literalenum.py is a the draft of the core runtime functionality (proposed to become
typing.LiteralEnumortyping_extensions.LiteralEnum) - src/literalenum is the full proposed PyPi module
- src/literalenum/mypy_plugin.py is an experimental mypy plugin
- src/literalenum/samples shows sample usage
- src/literalenum/stubgen.py provides tools for generating stubs, usable through CLI tool
lestub
Quickstart
Install
This repo is currently set up as a package under src/.
#python -m venv .venv
#source .venv/bin/activate
pip install literalenum
Realistic Current Usage
from typing import Literal
from literalenum import LiteralEnum
HttpMethodT = Literal["GET", "POST", "DELETE"]
class HttpMethod(LiteralEnum):
GET = "GET"
POST = "POST"
DELETE = "DELETE"
def handle(method: HttpMethodT) -> None:
print(f"{method=}")
handle("GET") # this should type-check ✅
handle(HttpMethod.GET) # this 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)
Optimistic Future Usage
from typing import LiteralEnum # NOT valid right now
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)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file literalenum-0.2.0.tar.gz.
File metadata
- Download URL: literalenum-0.2.0.tar.gz
- Upload date:
- Size: 41.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41bd7cd7d1b13e587e3c35c2ca4ee73791d4954bbc5ac7940d4b42ad02137e57
|
|
| MD5 |
2423520408d2c769fabcb4e2c2c1ad68
|
|
| BLAKE2b-256 |
268f1eff856630eaed1ee6bed8019e90302fb99b4b50e5f5aa6c4b21a8358ab1
|
Provenance
The following attestation bundles were made for literalenum-0.2.0.tar.gz:
Publisher:
publish.yml on modularizer/LiteralEnum
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
literalenum-0.2.0.tar.gz -
Subject digest:
41bd7cd7d1b13e587e3c35c2ca4ee73791d4954bbc5ac7940d4b42ad02137e57 - Sigstore transparency entry: 915122599
- Sigstore integration time:
-
Permalink:
modularizer/LiteralEnum@bce3a39b64289ac8919a591162e2149d0b0c4554 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/modularizer
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@bce3a39b64289ac8919a591162e2149d0b0c4554 -
Trigger Event:
release
-
Statement type:
File details
Details for the file literalenum-0.2.0-py3-none-any.whl.
File metadata
- Download URL: literalenum-0.2.0-py3-none-any.whl
- Upload date:
- Size: 29.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4588054f9ca1912fcdb1014dee3db79beda904c8ed3761e94b9a3bee5c77ced7
|
|
| MD5 |
4634b1010badb6eb2cd2ec2ffed8b1a3
|
|
| BLAKE2b-256 |
d21318a864445bbdcc10ffa7c9ba503864b1b3542a4e3e85343c95802983983e
|
Provenance
The following attestation bundles were made for literalenum-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on modularizer/LiteralEnum
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
literalenum-0.2.0-py3-none-any.whl -
Subject digest:
4588054f9ca1912fcdb1014dee3db79beda904c8ed3761e94b9a3bee5c77ced7 - Sigstore transparency entry: 915122680
- Sigstore integration time:
-
Permalink:
modularizer/LiteralEnum@bce3a39b64289ac8919a591162e2149d0b0c4554 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/modularizer
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@bce3a39b64289ac8919a591162e2149d0b0c4554 -
Trigger Event:
release
-
Statement type: