Skip to main content

Safe, non-error-raising, alternative to Pydantic validate_call decorator

Project description

validate-call-safe

validate_call_safe is a safe, non-error-raising alternative to Pydantic's validate_call decorator. It allows you to validate function arguments while gracefully handling validation errors through an error model, inspired by effects handlers, returning them as structured data models instead of raising exceptions.

This therefore means that side effects ('erroring') are transformed into return types. The return type annotation of a decorated function is modified accordingly as the Union of the existing return type with the provided error model type.

Features

  • Validates function arguments using Pydantic's existing validate_call decorator
  • Returns a custom error model instead of raising exceptions when validation fails
  • Configurable error information, including JSON representation and string representation of errors
  • Compatible with Pydantic v2, tested back to version 2.0.1
  • Optional model config and return value validation, as in the original Pydantic @validate_call decorator

Installation

pip install validate-call-safe

Usage

Basic Usage

Here's a basic example using a custom error model:

from pydantic import BaseModel, Json
from validate_call_safe import validate_call_safe

class CustomErrorModel(BaseModel):
    error_type: str
    error_json: Json
    error_str: str
    error_repr: str

@validate_call_safe(CustomErrorModel)
def int_noop(a: int) -> int:
    return a

success = int_noop(a=1)  # 1
failure = int_noop(a="A")  # CustomErrorModel(error_type='ValidationError', ...)

Return Value Validation

You can enable return value validation using the validate_return parameter:

@validate_call_safe(validate_return=True)
def int_noop(a: int) -> int:
    return "foo"  # This will cause a validation error

result = int_noop(a=1)  # ErrorModel(error_type='ValidationError', ...)

Error Model Configuration

validate_call_safe offers flexibility in specifying the error model:

  1. No brackets:

    @validate_call_safe
    def int_noop(a: int) -> int:
        return a
    
  2. Empty brackets:

    @validate_call_safe()
    def int_noop(a: int) -> int:
        return a
    
  3. Custom error model:

    @validate_call_safe(CustomErrorModel)
    def int_noop(a: int) -> int:
        return a
    
  4. With validation parameters:

    @validate_call_safe(validate_return=True)
    def int_noop(a: int) -> int:
        return a
    

Comparison with validate_call

With validate_call_safe you don't have to catch the expected ValidationError from Pydantic's validate_call:

# Using validate_call
from pydantic import validate_call

@validate_call
def unsafe_int_noop(a: int) -> int:
    return a

try:
    unsafe_int_noop(a="A")
except ValidationError as e:
    print(f"Error: {e}")

# Using validate_call_safe
from validate_call_safe import validate_call_safe

@validate_call_safe(CustomErrorModel)
def safe_int_noop(a: int) -> int:
    return a

result = safe_int_noop(a="A")
match result:
    case CustomErrorModel():
        print(f"Error: {result.error_type}")
    case int():
        ...  # Regular business logic here

Ideas for Future Development

  • Optionally restrict to ValidationError
  • Specify non-ValidationError exceptions to capture
  • Capture tracebacks

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

validate_call_safe-0.2.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

validate_call_safe-0.2.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: validate_call_safe-0.2.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.18.1 CPython/3.10.14 Linux/5.15.0-117-generic

File hashes

Hashes for validate_call_safe-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7370898d33ed3b1b4926690da34c4126c9a3838d416f738ef22cec34aa8c7197
MD5 15b98596d9a179d8059dc3e81ca4293f
BLAKE2b-256 42b950e23a996665c34be42dec4905a0d0d462afc2a8035f701236497ac28ab9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: validate_call_safe-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.18.1 CPython/3.10.14 Linux/5.15.0-117-generic

File hashes

Hashes for validate_call_safe-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a1fd8018418fe9296cb5a1b01ec513445b0fa7352d3e0d234dceb96d3ae4adad
MD5 45692a5f70ef1ebbcbf806e6fb6a2103
BLAKE2b-256 daaf94ee56bcf2d5f3a6a3c5f533e7a8d010ceb441589206f49fec6f1b2706ef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page