Skip to main content

Annotated-based refinement types for Python — with first-class Pydantic v2 integration. A practical projection of Σ-types into a language without dependent types.

Project description

refined-types

Annotated-based refinement types for Python — with first-class Pydantic v2 integration. A practical projection of Σ-types into a language without dependent types.

Status: 0.0.1 — name reservation. Real API ships in 0.1.0. Star/watch for updates.

What this is

A small library that gives you first-class refinement types — values that carry, at the type level, the evidence that a predicate was checked at the boundary. Built around Annotated[T, Refined[P]], with a tiny zero-dependency core and optional integrations for Pydantic v2 and Hypothesis.

# Coming in 0.1.0 — preview only

# 1. Standalone — no pydantic needed
from refined_types import Refined, Positive, NonEmpty, parse

def buy(quantity: Refined[int, Positive], item: Refined[str, NonEmpty]) -> None: ...

match parse(Positive, 5):
    case Ok(qty):   buy(qty, parse(NonEmpty, "apple").unwrap())
    case Err(why):  log.error(why)

# 2. Pydantic v2 — opt-in via `refined-types[pydantic]`
from pydantic import BaseModel
from refined_types import Refined, Range, Email

class User(BaseModel):
    email: Refined[str, Email]
    age:   Refined[int, Range[0, 150]]

Architecture

refined_types/
├── _core/          ← zero deps beyond typing_extensions
├── pydantic/       ← optional; needs `refined-types[pydantic]`
└── hypothesis/     ← optional; needs `refined-types[hypothesis]`

The core is framework-agnostic: works with plain Python, dataclasses, attrs, msgspec, or any other modeling layer. Pydantic v2 gets a first-class adapter — __get_pydantic_core_schema__ hook + JSON Schema constraint translation — but it is not a dependency of the core.

Why not phantom-types?

phantom-types is a great library that occupies this niche. We differ on the axes that matter for Pydantic-v2-native, type-checker-strict workflows:

Aspect phantom-types refined-types
DSL inheritance Annotated[T, Refined[P]]
Core dependencies typeguard, numerary typing-extensions only
Pydantic v1 hook (legacy) v2 native (__get_pydantic_core_schema__)
Type-checker distinctness weak (subclass of bound) strong (NewType-discriminated)
Parse failure raises raises and try_parse() -> Result
Predicate algebra functions: both(p, q) operators: P & Q
Refined collections safe API absent NonEmpty.head() -> T
Σ-type framing absent first-class in docs

Connection to type theory

In MLTT/HoTT, a Σ-type is Σ(x : T). P(x) — a value of type T paired with a proof that P(x) holds. In a language without dependent types, we keep the shape of Σ: a smart constructor is the only entry point, and the resulting type carries (proof-irrelevantly) the witness that the predicate was checked.

This is not full Σ — Python cannot express type-level computation, cross-field dependent constraints, or proof-relevant equality. But the discipline Σ encourages — parse-don't-validate, illegal states unrepresentable, transport guarantees through function signatures — is exactly what this library gives.

Installation

# Core only — no pydantic, no hypothesis
pip install refined-types

# With Pydantic v2 integration
pip install "refined-types[pydantic]"

# With Hypothesis strategy registration
pip install "refined-types[hypothesis]"

# Everything
pip install "refined-types[all]"

Roadmap

  • 0.0.1 — name reservation (you are here)
  • 0.1.0Refined[T, P] core, smart constructors with Result, built-in numeric/string/collection predicates, Pydantic v2 adapter, JSON Schema
  • 0.2.0 — predicate algebra (&, |, ~), refined collections with safe API
  • 0.3.0 — Hypothesis strategy registration, FastAPI recipes, attrs/msgspec integration examples
  • 1.0.0 — stable API, full docs site

License

MIT — see LICENSE.

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

refined_types-0.1.0a1.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

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

refined_types-0.1.0a1-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file refined_types-0.1.0a1.tar.gz.

File metadata

  • Download URL: refined_types-0.1.0a1.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for refined_types-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 42b301b4c7d39c27e5f93fa25a9b00f56390dee5d77f7ff412befc2fbf207fda
MD5 461c3bd299f51d5d6a73d07ebf1485fc
BLAKE2b-256 392707018a3b5c362ceff3974792593a9836fb6affbeef5582bbb24a41323c5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for refined_types-0.1.0a1.tar.gz:

Publisher: publish.yml on m0n0x41d/refined-types

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

File details

Details for the file refined_types-0.1.0a1-py3-none-any.whl.

File metadata

  • Download URL: refined_types-0.1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for refined_types-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 d4edf6379ec4d637fe778b006a87cc955c2720e102f2a008230057fd9ceafe68
MD5 24313860c609d9829e9e2cfdbbde0a35
BLAKE2b-256 f10945f6f342399ceb540fcf2859e8f5b903ca12e01c6214b79233a18cfeab67

See more details on using hashes here.

Provenance

The following attestation bundles were made for refined_types-0.1.0a1-py3-none-any.whl:

Publisher: publish.yml on m0n0x41d/refined-types

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