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 validate_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
ValidationErroron 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
validate_array(
array,
*,
dtype=None,
shape=None,
writable=None,
contiguous=None,
)
Validate NumPy array properties without copying or modifying the array.
validate_array(x, dtype=np.float64, shape=(100,))
validate_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.1.0 (not yet released)
Project details
Release history Release notifications | RSS feed
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 requireit-0.1.0.tar.gz.
File metadata
- Download URL: requireit-0.1.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7843b4b435064ece2afe4377bb54882c9dced7a3657067d8387dc32cfaf34d7e
|
|
| MD5 |
526addc4ae7a180fbadaf756e778cf61
|
|
| BLAKE2b-256 |
1d706a14dbbd95d705841554d012139461b9b339214852f073975b0d92fb2998
|
Provenance
The following attestation bundles were made for requireit-0.1.0.tar.gz:
Publisher:
ci.yml on mcflugen/requireit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
requireit-0.1.0.tar.gz -
Subject digest:
7843b4b435064ece2afe4377bb54882c9dced7a3657067d8387dc32cfaf34d7e - Sigstore transparency entry: 815325268
- Sigstore integration time:
-
Permalink:
mcflugen/requireit@b50e9246032fc44eb1e10333beaf637c623b8ad0 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/mcflugen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@b50e9246032fc44eb1e10333beaf637c623b8ad0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file requireit-0.1.0-py3-none-any.whl.
File metadata
- Download URL: requireit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.7 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 |
88f101ecb491c2d0204071028f7547ce8b102bed722b0abea021ab7b0a05dc95
|
|
| MD5 |
ba7de0930ab8b800aaa09fc229d3c512
|
|
| BLAKE2b-256 |
fb56ed9dfab18742fb9910979a1b1e8c20e89f3b86d8e81869454799df457c49
|
Provenance
The following attestation bundles were made for requireit-0.1.0-py3-none-any.whl:
Publisher:
ci.yml on mcflugen/requireit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
requireit-0.1.0-py3-none-any.whl -
Subject digest:
88f101ecb491c2d0204071028f7547ce8b102bed722b0abea021ab7b0a05dc95 - Sigstore transparency entry: 815325278
- Sigstore integration time:
-
Permalink:
mcflugen/requireit@b50e9246032fc44eb1e10333beaf637c623b8ad0 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/mcflugen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@b50e9246032fc44eb1e10333beaf637c623b8ad0 -
Trigger Event:
push
-
Statement type: