Install & Compatibility
Where this runs
tested against v1.0.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.038s · 18MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 1.6s · import 0.035s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
accepts
✓ from pyvalid import accepts, returns
returns
✓ from pyvalid import accepts, returns
This example demonstrates how to use the `@accepts` and `@returns` decorators to validate function arguments and return values. It also includes basic error handling for the exceptions raised by `pyvalid`.
from pyvalid import accepts, returns, InvalidArgumentNumberError, ArgumentValidationError, InvalidReturnType
@returns(int, float)
@accepts(str, (int, 2.0), (int, float))
def calc(operator, val1, val2, val3):
# WARNING: Using eval() with untrusted input is a security risk.
# This is for demonstration purposes based on the official example.
expression = '{v1} {op} {v2} {op} {v3}'.format(
op=operator, v1=val1, v2=val2, v3=val3
)
return eval(expression)
try:
# Returns int value: 24
print(calc('*', 2, 3, 4))
# Returns float value: 24.0
print(calc(operator='*', val1=2, val2=3.0, val3=4))
# This will raise an ArgumentValidationError because 'val1' should be int or 2.0, not a string
# print(calc('+', 1, 'invalid', 3))
# Example of a call that would fail if uncommented and demonstrate exception handling
# calc('+', 1, 'invalid_type', 3)
# Example that would fail if return type is wrong
# @returns(str)
# def bad_return():
# return 123
# bad_return()
except (InvalidArgumentNumberError, ArgumentValidationError, InvalidReturnType) as e:
print(f"Validation Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
Debug
Known issues
gotchaThe library has not been updated since October 2020. This indicates potential for limited or no support for newer Python versions (e.g., Python 3.10 and above) or a slower response to bug fixes and community contributions.fixThoroughly test `pyvalid` in your target Python environment (3.10+) before relying on it in production. Consider actively maintained alternatives for new projects.
affects: 1.0.4 (potentially Python versions >3.9)
gotchaThe official quickstart example uses `eval()` with string interpolation. If the `operator` or any other input parameters in such a context were to come from untrusted user input, it could lead to a severe security vulnerability allowing arbitrary code execution.fixAvoid using `eval()` with any unvalidated or untrusted input. Implement explicit parsing and conditional logic for operations instead of `eval()`.
affects: All versions
gotchaThe quickstart examples primarily focus on decorator usage but do not explicitly demonstrate how to catch the specific exceptions (`InvalidArgumentNumberError`, `ArgumentValidationError`, `InvalidReturnType`) raised by `pyvalid` upon validation failure. Lack of proper exception handling can lead to ungraceful program termination.fixAlways wrap calls to functions decorated with `pyvalid` in `try-except` blocks, catching `pyvalid`'s specific exception types to handle validation failures gracefully.
affects: All versions
Errors
Common errors & fixes
TypeError: Argument 'number' must be of type int, but got float.
A function argument passed to a pyvalid-decorated function did not match the expected type specified in the `@accepts` decorator.
fixEnsure the argument passed to the function matches the type declared in the `@accepts` decorator. For example, pass an integer where `int` is expected, or update the decorator to accept the correct type.
ValueError: Argument 'value' must satisfy condition 'is_positive'.
A custom validator function specified for an argument in the `@accepts` decorator returned `False`, indicating the validation condition was not met.
fixEnsure the argument satisfies the condition defined by the custom validator function. Check the validator's logic and the input data being passed.
TypeError: Return value must be of type str, but got int.
The value returned by a pyvalid-decorated function did not match the expected type specified in the `@returns` decorator.
fixModify the function to return a value of the type declared in the `@returns` decorator, or update the decorator to expect the function's actual return type.
ModuleNotFoundError: No module named 'pyvalid'
The `pyvalid` library is not installed in the current Python environment, or the environment is not correctly activated.
fixInstall the library using pip: `pip install pyvalid`
Upgrade
Version history
1.0.4latest on PyPI · released Oct 13, 2020
Audit
Dependencies
No dependency data recorded yet.