Skip to main content

Automatic Pydantic model generation and validation for function parameters and return types.

Project description

auto-pydantic

  • Author : Sonnet3.5
  • Editor : Sisung Kim

auto-pydantic is a Python module that provides automatic Pydantic model generation and validation for function parameters and return types.

Features

  • Generate Pydantic input models from function signatures
  • Generate Pydantic output models from function return types
  • Automatic validation of function inputs using generated Pydantic models
  • Support for simple and complex function signatures, including *args and **kwargs

Installation

To install crimson-auto-pydantic, you can use pip:

pip install crimson-auto-pydantic

Usage

Generating Input Props

from crimson.auto_pydantic.generator import generate_input_props

def my_function(arg1: int, arg2: str = "default") -> str:
    return f"{arg1} {arg2}"

input_props = generate_input_props(my_function)
print(input_props)

Output:

class MyFunctionInputProps(BaseModel):
    arg1: int = Field(...)
    arg2: str = Field(default="'default'")

    def __init__(self, arg1: int, arg2: str='default'):
        super().__init__(arg1=arg1, arg2=arg2)

Generating Output Props

from crimson.auto_pydantic.generator import generate_output_props

def my_function(arg1: int, arg2: str = "default") -> str:
    return f"{arg1} {arg2}"

output_props = generate_output_props(my_function)
print(output_props)

Output:

class MyFunctionOutputProps(BaseModel):
    return: str

Validating Function Inputs

from crimson.auto_pydantic.validator import validate
from inspect import currentframe

def my_function(arg1: int, arg2: str = "default") -> str:
    validate(my_function, currentframe(), arg1, arg2)
    return f"{arg1} {arg2}"

# This will pass validation
my_function(1, "test")

# This will raise a validation error
my_function("not an int", "test")

Validation Error:

ValidationError: 1 validation error for MyFunctionInputProps
arg1
  Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='not an int', input_type=str]
    For further information visit https://errors.pydantic.dev/2.8/v/int_parsing

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

crimson_auto_pydantic-0.1.9.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

crimson_auto_pydantic-0.1.9-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file crimson_auto_pydantic-0.1.9.tar.gz.

File metadata

  • Download URL: crimson_auto_pydantic-0.1.9.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for crimson_auto_pydantic-0.1.9.tar.gz
Algorithm Hash digest
SHA256 b06568f2fccb5cec3f49d4147c0efd7a00ac691b485407eee5aeac784c20fa40
MD5 d5474fe8b1d4aab2056ee4494da95eb2
BLAKE2b-256 487bc202d83ed6f2638e844c11a32f33c219be43d9cef9893c70d177f68e6b65

See more details on using hashes here.

File details

Details for the file crimson_auto_pydantic-0.1.9-py3-none-any.whl.

File metadata

File hashes

Hashes for crimson_auto_pydantic-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 a8bf9f6719af3a0478e759dd6e1ed8f6a13c217ccc9c05121b0b616c2a290309
MD5 8b8eac43df5b953247b7b52d111d6a5c
BLAKE2b-256 e0b5441ea5318b1a0971a4e9c45141377691484697618f4f45d41ffbefabc998

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