Skip to main content

A Python library for enforcing type, range, and lexical constraints.

Project description

Constrained

:caution: Not working; not maintained. Use annotated-types instead. :caution:

A Python library for enforcing type and range constraints.

Features

  • Float and Int types with range constraints
  • Regex type for string matching
  • CFG type for context-free grammar matching
  • enforce_annotation_constraints decorator to enforce constraints

Installation

pip install constrained

Usage

First, import the necessary types and the decorator from the constrained module:

from constrained import Float, Int, Regex, CFG, enforce_annotation_constraints

Then, define your function with type annotations:

@enforce_annotation_constraints
def P(x: Float[0, 'inf']) -> Float[0, 'inf']:
    return x / 2

The decorator will enforce the constraints at runtime and raise a ValueError if any argument or the return value does not match the constraints defined in the function's signature.

Alternatives

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

constrained-0.0.0.tar.gz (3.2 kB view hashes)

Uploaded Source

Built Distribution

constrained-0.0.0-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

Supported by

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