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, with no required dependencies. NumPy is optional for array validation.
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
Numeric checks work with Python real scalars (including integers and floats) without NumPy. For array-like inputs and array validators, install the NumPy extra:
pip install 'requireit[numpy]'
API Summary
All validators:
- validate the first argument
- return the original value/array on success
- raise
ValidationErroron failure
Arrays
require_array: Validate an array to satisfy requirements.require_dtype: Validate that an array has a required dtype or can be safely cast to it.require_like: Validate that an array has the same shape and/or dtype as another.require_ndim: Validate that an array has a specific number of dimensions.require_shape: Validate that an array has the specified shape.require_sorted: Validate that an array is sorted.
General
require_contains: Requirecollectioncontains required values.require_contains_exactly: Requirecollectioncontains exactly the expected members.require_does_not_contain: Requirecollectioncontains no forbidden members.require_instance: Requirevalueis an instance of one or more types.require_members: Requirecollectioncontains/does not contain members.require_none: RequirevalueisNone.require_not_none: Requirevalueis notNone.require_not_one_of: Requirevalueis not contained inforbiddenrequire_one_of: Requirevalueis contained inallowed
Length
require_length: Requirelen(value) == lengthrequire_length_at_least: Requirelen(value) >= lengthrequire_length_at_most: Requirelen(value) <= lengthrequire_length_between: Requirelen(value)falls within a specified range.
Numeric
Numeric validators accept real scalar values or, with NumPy installed, array-like
values. For arrays, every element must satisfy the check. Bounds must be real
scalars, including NumPy integer and floating scalars; array-valued bounds are
not supported. In require_between, None means that a bound is omitted.
NaN input values raise ValidationError. NaN or non-real-scalar bounds raise
ValueError.
require_between: Validate that a value lies within a specified interval.require_greater_than: Requirevalue > lowerrequire_greater_than_or_equal: Requirevalue >= lowerrequire_less_than: Requirevalue < upperrequire_less_than_or_equal: Requirevalue <= upperrequire_negative: Requirevalue < 0require_nonnegative: Requirevalue >= 0require_nonpositive: Requirevalue <= 0require_positive: Requirevalue > 0
Paths
require_path_string: Validate that a value is a string intended to be used as a path.
Command-line integration
argparse_type: Adapt a requireit validator for use as an argparsetype=callable.
import argparse
from requireit import argparse_type, require_positive
def parse_positive_int(value: str) -> int:
return require_positive(int(value))
parser = argparse.ArgumentParser()
parser.add_argument("--count", type=argparse_type(parse_positive_int))
Converts ValidationError into argparse.ArgumentTypeError, allowing requireit
validators to produce clean command-line error messages.
Errors
All validation failures raise ValidationError, which inherits from both
RequireItError and the standard ValueError:
requireit.ValidationError
This allows callers to catch validation failures distinctly from other errors.
To adapt validation errors to another exception type, use raise_as:
from requireit import raise_as
from requireit import require_positive
with raise_as(ValueError):
require_positive(-1)
This raises:
ValueError: value must be positive
Useful when integrating requireit into APIs that already expose a specific exception type.
raise_as also accepts an optional note, attached to the raised exception
via add_note:
with raise_as(ValueError, note="while parsing config.toml"):
require_positive(-1)
To attach a note without changing the exception type, use add_note directly:
from requireit import add_note
with add_note("while parsing config.toml"):
require_positive(-1)
This still raises ValidationError, with the note included in the traceback.
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.12.0 (2026-10-05)
Features
- Added
require_membersto combine required, allowed, and forbidden membership constraints. #63 - Added
require_contains_exactlyandrequire_does_not_contain. #63
Changes
require_one_ofandrequire_not_one_ofno longer support unhashable values or unhashable items inallowedorforbidden. These inputs now raiseTypeError. #64- NumPy is now optional. Numeric validators support Python real scalars without
NumPy, and array validation loads NumPy only when needed. Install
requireit[numpy]to use array-like inputs or array validators. #62
Fixes
require_betweenand the numeric validators built on it now reject NaN bounds withValueErrorfor both scalar and array inputs. #62
0.11.0 (2026-08-14)
Changes
ValidationErrornow also inherits from the standardValueError, allowing callers to handle requireit validation failures with other invalid values. #58require_length,require_length_at_least,require_length_at_most, andrequire_length_betweennow raiseTypeErrorwhen passed a value that does not have a length. #58
0.10.1 (2026-08-13)
Fixes
require_between, and the validators built on it (require_positive,require_negative,require_nonnegative,require_nonpositive,require_greater_than,require_greater_than_or_equal,require_less_than, andrequire_less_than_or_equal), now rejectnanrather than silently treating it as satisfying the bound. #56
0.10.0 (2026-07-28)
Features
- Added
require_liketo validate that an array matches another array's shape and/or dtype. #46 - Added
require_ndimto validate that an array has a required number of dimensions. #47 - Added
require_noneandrequire_not_noneto validate that a value is, or is not,None. #50 - Added
add_notecontext manager to attach a note to aRequireItErrorraised within its block, and anotekeyword toraise_asto do the same when converting aValidationErrorto another exception type. #51
Changes
- Dropped support for Python 3.11. #52
Fixes
- Fixed
require_dtypeerror messages for NumPy dtype families such asnp.floating. #45
Tests
- Cleaned up the parametrized
requiretests by collecting the failing and passing cases into namedCHECKS_THAT_FAIL/CHECKS_THAT_PASSdicts. #49
0.9.0 (2026-04-23)
Features
- Added
require_instanceto check that a value is an instance of a type. #42
Fixes
- Fixed CI test jobs so macOS runners use the Python version selected by
actions/setup-python. #43
0.8.0 (2026-04-14)
Features
- Added
raise_ascontext manager to re-raiseValidationErroras a user-specified exception type. #40
0.7.0 (2026-04-12)
Features
- Added
require_sortedto check that values are sorted in ascending order. #35 - Added
require_dtypeto check that values have a given dtype or, optionally, can be safely cast to that dtype. #36
Changes
- Dropped support for Python 3.10. #37
0.6.0 (2026-04-01)
Features
- Allow the
dtypekeyword ofrequire_arrayto accept numpy dtype families such asnp.integerandnp.floatingin addition to exact dtypes. #32
0.5.0 (2026-03-28)
Features
- Extended
require_arrayto allow flexible shape validation with support for wildcard dimensions (None or named axes). #29
0.4.0 (2026-03-27)
Features
- Added
require_greater_than,require_greater_than_or_equal, andrequire_less_than_or_equalvalidators. #26
0.3.0 (2026-03-23)
Features
- Added
require_not_one_ofvalidator to ensure a value is not in a forbidden set #15 - Added length validators to check that an object’s length is exactly, at most, or at least a given value #16
- Added
require_length_betweenvalidator to check that an object’s length is within a specified range #19 - Added
require_containsvalidator to ensure a collection contains required values #21 - Added
import_packagevalidator to check for and import a package #22 - Added
argparse_typeto allow requireit validators to be used asargparsetype=callables #17
Changes
- Renamed length validators for consistency:
require_length_is→require_length,require_length_is_at_least→require_length_at_least,require_length_is_at_most→require_length_at_most#20
Tests
- Added unit tests to verify that validators return the input value (not a copy) on success #18
0.2.0 (2026-01-16)
- Standardized validation error messages #8
- Renamed
validate_arraytorequire_array#9 - Added optional
namekeyword 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)
Metadata
Release files for requireit 0.12.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| requireit-0.12.0.tar.gz | 14.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| requireit-0.12.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.4 kB
Release files / requireit-0.12.0.tar.gz
| Download URL | requireit-0.12.0.tar.gz |
|---|---|
| Size | 14.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ebe6800e57d452bef88ec12a2b3f03a20a87ff36c5d294237010d68a8de72cc1
|
|
BLAKE2b-256 checksum How to use checksums |
9bee7d305136db84dd5abc32e78f9bf0bcd1a5b938083eaa8627bc2cf5aa1408
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.
Transparency logRelease files / requireit-0.12.0-py3-none-any.whl
| Download URL | requireit-0.12.0-py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4a5aa916b12d7e98cd7441bf5d9985cee3f7838e8da199b11cd719dad67d5842
|
|
BLAKE2b-256 checksum How to use checksums |
765a0aabca0b08b56001987cefe2f2aef5c8af04cce4de28376162a3104d7c64
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.
Transparency log