{"id":6840,"library":"pyvalid","title":"pyvalid","description":"pyvalid is a Python data validation tool that allows for easy validation of function's input parameters and return values using `accepts` and `returns` decorators. The current version is 1.0.4, released in October 2020, suggesting a low release cadence.","status":"active","version":"1.0.4","language":"en","source_language":"en","source_url":"https://github.com/eterey/pyvalid","tags":["validation","decorator","type checking","runtime validation","function arguments","return values"],"install":[{"cmd":"pip install pyvalid","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"accepts","correct":"from pyvalid import accepts, returns"},{"symbol":"returns","correct":"from pyvalid import accepts, returns"}],"quickstart":{"code":"from pyvalid import accepts, returns, InvalidArgumentNumberError, ArgumentValidationError, InvalidReturnType\n\n@returns(int, float)\n@accepts(str, (int, 2.0), (int, float))\ndef calc(operator, val1, val2, val3):\n    # WARNING: Using eval() with untrusted input is a security risk.\n    # This is for demonstration purposes based on the official example.\n    expression = '{v1} {op} {v2} {op} {v3}'.format(\n        op=operator, v1=val1, v2=val2, v3=val3\n    )\n    return eval(expression)\n\ntry:\n    # Returns int value: 24\n    print(calc('*', 2, 3, 4))\n\n    # Returns float value: 24.0\n    print(calc(operator='*', val1=2, val2=3.0, val3=4))\n\n    # This will raise an ArgumentValidationError because 'val1' should be int or 2.0, not a string\n    # print(calc('+', 1, 'invalid', 3))\n\n    # Example of a call that would fail if uncommented and demonstrate exception handling\n    # calc('+', 1, 'invalid_type', 3)\n\n    # Example that would fail if return type is wrong\n    # @returns(str)\n    # def bad_return():\n    #    return 123\n    # bad_return()\n\nexcept (InvalidArgumentNumberError, ArgumentValidationError, InvalidReturnType) as e:\n    print(f\"Validation Error: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"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`."},"warnings":[{"fix":"Thoroughly test `pyvalid` in your target Python environment (3.10+) before relying on it in production. Consider actively maintained alternatives for new projects.","message":"The 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.","severity":"gotcha","affected_versions":"1.0.4 (potentially Python versions >3.9)"},{"fix":"Avoid using `eval()` with any unvalidated or untrusted input. Implement explicit parsing and conditional logic for operations instead of `eval()`.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always wrap calls to functions decorated with `pyvalid` in `try-except` blocks, catching `pyvalid`'s specific exception types to handle validation failures gracefully.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}