phantom-types
Phantom types for Python
What it is and what it does
Phantom types let you attach runtime predicates to Python types so that illegal states become unrepresentable at both the type-checking and runtime levels. Instead of validating data after parsing (shotgun parsing), you create a phantom type—a subclass of a base type like `str` with a predicate—and parse data into that type upfront. The type checker then treats the result as proven to satisfy the predicate, and runtime validators like typeguard or beartype can enforce the same constraint at call boundaries.
The package integrates with pydantic for schema generation and validation, works with static type checkers like mypy, and supports optional extras for datetime, phone number, and hypothesis-based testing. It requires Python 3.9+ and depends on typeguard, typing-extensions, and numerary at runtime.
Use it for:
- Enforce string constraints (e.g., non-empty, from a fixed set) at the type level so functions can assume preconditions without defensive checks.
- Combine with pydantic models to validate and document API request/response schemas with phantom type predicates.
- Use with beartype or typeguard to add runtime pre- and post-condition checking to functions without explicit guard clauses.
- Parse and validate user input once at the boundary, then pass phantom-typed values through your codebase with static proof of validity.
- Generate JSONSchema from pydantic models that include phantom types, automatically documenting constraints in API specs.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Phantom types enable you to create type-safe wrappers around base types (like strings or integers) with runtime predicates, allowing static type checkers and runtime validators to enforce constraints without shotgun parsing.
Yes, if you want to move validation earlier in your pipeline and leverage static type checking to prevent invalid states. The package is stable (Production/Stable classifier), has low install friction, and works well with pydantic and runtime type checkers. The 656-day gap since last release is a minor concern for long-term maintenance, but the repository remains active and there are no known vulnerabilities. Install it if your codebase values type safety and parse-don't-validate patterns.
Install
phantom-types on PyPI
pip
pip install phantom-typesuv
uv add phantom-typespoetry
poetry add phantom-typesInstalling phantom-types
Before you install
Low install friction; pure Python wheel with three runtime dependencies (typeguard, typing-extensions, numerary). Repository shows aging maintenance—last release was 656 days ago—but the project remains active with recent commits and no archived status.
License in practice
BSD-3-Clause (permissive) allows commercial use, modification, and distribution with minimal restrictions, making it suitable for most projects.
Quickstart
pip install phantom-types
from phantom import Phantom
from phantom.predicates.collection import contained
class Name(str, Phantom, predicate=contained({"Jane", "Joe"})): ...
name = Name.parse("Jane")
print(f"Valid: {name}")
Requires Python 3.9 or later; optional extras (pydantic, hypothesis, dateutil, phonenumbers) require those packages to be installed separately.
Verify before relying
- Whether runtime validation overhead is acceptable for performance-critical code paths.
- How well phantom types integrate with type checkers other than mypy (e.g., pyright, pyre).
- Whether the 656-day gap since last release indicates active maintenance or dormancy.
Package facts
| License | BSD-3-Clause (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 3 — typeguard, typing-extensions, numerary |
| Maintenance | aging — 656 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 79,453/month — #14,356 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: phantom_types-3.0.2-py3-none-any.whl
Tags
More Quality Assurance packages
Coverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
ruffRuff is a Python linter and code formatter…
permissive · top 1,000 on PyPI
pexpectPexpect spawns and controls interactive console…
permissive · top 1,000 on PyPI
blackBlack reformats Python source code to a…
permissive · top 1,000 on PyPI
pytest-xdistpytest-xdist distributes pytest tests across…
permissive · top 1,000 on PyPI
cfn-lintValidates AWS CloudFormation templates in YAML…
permissive · top 1,000 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typepyTypepy checks, validates, and converts Python…
permissive · top 5,000 on PyPI
beartypeBeartype is a runtime type checker that…
permissive · top 1,000 on PyPI
datetypeDateType provides type-checking-time wrapper…
unclear · top 15,000 on PyPI
typeguardPerforms run-time type checking for Python…
permissive · top 1,000 on PyPI
runtypeRuntype provides runtime type validation,…
permissive · top 15,000 on PyPI
types-jsonschemaProvides type hints for the jsonschema package,…
permissive · top 5,000 on PyPI
type-enforcedEnforces Python type annotations at runtime…
permissive · top 15,000 on PyPI
DateTimeProvides a DateTime data type for working with…
unclear · top 5,000 on PyPI
protoc-gen-validateValidates protocol buffer messages against…
unclear · top 15,000 on PyPI