Skip to main content

Tiny runtime validators for explicit precondition checks

Project description

requireit

Tiny, numpy-aware runtime validators for explicit precondition checks.

requireit provides a small collection of lightweight helper functions such as require_positive, require_between, and require_array for validating values and arrays at runtime.

It is intentionally minimal and dependency-light (numpy only).

Why requireit?

  • Explicit – reads clearly
  • numpy-aware – works correctly with scalars and arrays
  • Fail-fast – raises immediately with clear error messages
  • Lightweight – just a bunch of small functions
  • Reusable – avoids copy-pasted validation code across projects
from requireit import require_one_of
from requireit import require_positive

require_positive(dt)
require_one_of(method, allowed={"foo", "bar"})

Design principles

  • Prefer small, single-purpose functions
  • Raise standard exceptions (ValidationError)
  • Never coerce or "fix" invalid inputs
  • Validate all elements for array-like inputs
  • Keep the public API small

Non-goals

requireit is not:

  • a schema or data-modeling system
  • a replacement for static typing
  • a validation framework
  • a substitute for unit tests
  • a coercion or parsing library

If you need structured validation, transformations, or user-facing error aggregation, you probably want something heavier.

Installation

pip install requireit

API overview

All validators:

  • return the original value/array on success
  • raise ValidationError on failure

Membership validation

require_one_of(value, *, allowed)

Validate that a value is one of a set of allowed values.

require_one_of("foo", allowed=("foo", "bar"))  # ok, returns "foo"
require_one_of("baz", allowed=("foo", "bar"))  # raises ValidationError

Range validation

require_between(
    value,
    a_min=None,
    a_max=None,
    *,
    inclusive_min=True,
    inclusive_max=True,
)

Validate that a scalar or array lies within specified bounds.

  • Bounds may be inclusive or strict
  • Validation fails if any element violates the constraint
require_between([0, 1], a_min=0.0) # ok, returns [0, 1]
require_between([0, 1], a_min=0.0, inclusive_min=False) # raises

Sign-based helpers

Convenience wrappers around require_between:

require_positive(value)  # > 0
require_nonnegative(value)  # >= 0
require_negative(value)  # < 0
require_nonpositive(value)  # <= 0

All accept scalars or array-like inputs.

Array validation

require_array(
    array,
    *,
    dtype=None,
    shape=None,
    writable=None,
    contiguous=None,
)

Validate NumPy array properties without copying or modifying the array.

require_array(x, dtype=np.float64, shape=(100,))
require_array(x, writable=True, contiguous=True)

Checks are applied only if the corresponding keyword is provided.

Errors

All validation failures raise:

requireit.ValidationError

This allows callers to catch validation failures distinctly from other errors.

Contributing

This project is intentionally small.

Contributions should preserve:

  • minimal surface area
  • explicit semantics
  • no additional dependencies

If a proposed change needs much explanation, it probably doesn’t belong here.

Credits

Development Leads

Release Notes

0.2.0 (2026-01-16)

  • Standardized validation error messages #8
  • Renamed validate_array to require_array #9
  • Added optional name keyword to require functions to make error messages easier to read #10
  • Added new validator, require_path_string, that checks if a value could be used as a file path #11
  • Added new validator, require_less_than, that checks if one value is less than another #12

0.1.0 (2026-01-12)

  • Added documentation to the README #1
  • Added project metadata files #2
  • Added the requireit module #3
  • Added pyproject.toml file #4
  • Added noxfile.py file and linters #5
  • Added unit tests for requireit #6
  • Added GitHub Actions for CI #7

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

requireit-0.2.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

requireit-0.2.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file requireit-0.2.0.tar.gz.

File metadata

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

File hashes

Hashes for requireit-0.2.0.tar.gz
Algorithm Hash digest
SHA256 61f283102649483c5de501a82372afd81600f5e62cb1ba05c1f8c056d5ceabba
MD5 92d4d24d8fb01bd6a7511024682b213e
BLAKE2b-256 97cd79dfee9be2152e7212a924b158e087f82e2c8a500f5e4c9e25726e5e2da3

See more details on using hashes here.

Provenance

The following attestation bundles were made for requireit-0.2.0.tar.gz:

Publisher: ci.yml on mcflugen/requireit

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

File details

Details for the file requireit-0.2.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for requireit-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cca068e0d353765ebb3713979704c0328e6752ba5141a6cc34369cb59a5427bf
MD5 72c9ac666316dd8579045bc59a18a0d4
BLAKE2b-256 6e91c6df3ed3045ce2eee317b547290826bbf639a0a9ff0600c2a83bc7db32d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for requireit-0.2.0-py3-none-any.whl:

Publisher: ci.yml on mcflugen/requireit

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