Migrating v1 Pydantic ValidatedFunction to v2.
Project description
Pydantic Function Models
A library to model Python function signatures using Pydantic, inspired by the now-deprecated ValidatedFunction from Pydantic v1. This package helps validate function arguments according to type hints, bridging the gap between Pydantic v1 and v2.
Installation
pip install pydantic-function-models
Features
- Signature modelling: Wrap any Python function to model its signature (positional, keyword, etc.) using Pydantic. Note that if you are after just validation (whether the arguments are valid) then you want validate call
- Painless migration from Pydantic v1: Provides a
ValidatedFunctionclass ported to v2, for those who relied onValidatedFunctionin v1. - Clear error messages: Raises Pydantic
ValidationErrorwith helpful details if input arguments do not match the declared types. - Reserved name checks: Prevents usage of special parameter names (e.g.,
v__args,v__kwargs) that could conflict with internal validation logic.
Usage
Below is a general overview of how to use pydantic-function-models in your own code. There is no built-in command-line interface; usage is purely through Python imports.
Command-Line Interface
This library does not provide a CLI. All functionality is accessed by importing and using its classes in Python code. The remaining “Examples” section demonstrates typical usage patterns.
Example: Basic Validated Function
from pydantic_function_models import ValidatedFunction
def add(a: int, b: int) -> int:
return a + b
vf = ValidatedFunction(add)
# Build arguments for validation
args_to_validate = (1,)
kwargs_to_validate = {"b": 2}
validated = vf.model.model_validate({
"a": args_to_validate[0],
"b": kwargs_to_validate["b"]
})
result = add(**validated.model_dump(exclude_unset=True))
print(result) # 3
Here, ValidatedFunction creates an internal Pydantic model that enforces integer values for a and b. If you passed a string for a or b, it would raise a ValidationError.
Library Usage
Internally, each ValidatedFunction builds a Pydantic model representing the function’s signature. When you call .model.model_validate(...) or .build_values(...), it validates the provided arguments (whether positional or keyword-based) against the signature.
Project Structure
Take a look at the internals here:
validated_function.py: Core logic forValidatedFunction, which encapsulates argument validation and Pydantic model generation.parameters.py: Model definitions for function parameters/signatures, with checks for reserved argument names.
Contributing
Maintained by lmmx. Contributions are welcome!
- Issues & Discussions: Please open a GitHub issue or discussion for bugs, feature requests, or questions.
- Pull Requests: PRs are welcome!
- Install the dev extra (e.g. with uv:
uv pip install -e .[dev]) - Run tests (e.g.
pytest) and include updates to docs or examples if relevant. - If reporting a bug, please include the version and error message/traceback if available.
- Install the dev extra (e.g. with uv:
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_function_models-0.1.11.tar.gz.
File metadata
- Download URL: pydantic_function_models-0.1.11.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
28292961bc71f9e4d75ae608ef1cf820ce650ba019067776ee82c6612ccf1cca
|
|
| MD5 |
2f148bd0d4bdbed1f98dc9494da60fb7
|
|
| BLAKE2b-256 |
ba9f9f89602abf782693974d1812657b7b0bab346c011f31d7a05ca71f5643e2
|
Provenance
The following attestation bundles were made for pydantic_function_models-0.1.11.tar.gz:
Publisher:
CI.yml on lmmx/pydantic-function-models
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_function_models-0.1.11.tar.gz -
Subject digest:
28292961bc71f9e4d75ae608ef1cf820ce650ba019067776ee82c6612ccf1cca - Sigstore transparency entry: 731727073
- Sigstore integration time:
-
Permalink:
lmmx/pydantic-function-models@8ca61797061643eba431730735538da9c7e300d0 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/lmmx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
CI.yml@8ca61797061643eba431730735538da9c7e300d0 -
Trigger Event:
workflow_run
-
Statement type:
File details
Details for the file pydantic_function_models-0.1.11-py3-none-any.whl.
File metadata
- Download URL: pydantic_function_models-0.1.11-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c17d45f7b7b95ad1644226a9b8d6d05ce1565a0d0bbe03f4ec86e21487aff2b
|
|
| MD5 |
260abf164526feb7fad202bc3f1aaa61
|
|
| BLAKE2b-256 |
8b5ccd8bd73e893604e335f4347539851a8f8dede32fbf6d7a009f564c2c6681
|
Provenance
The following attestation bundles were made for pydantic_function_models-0.1.11-py3-none-any.whl:
Publisher:
CI.yml on lmmx/pydantic-function-models
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_function_models-0.1.11-py3-none-any.whl -
Subject digest:
1c17d45f7b7b95ad1644226a9b8d6d05ce1565a0d0bbe03f4ec86e21487aff2b - Sigstore transparency entry: 731727074
- Sigstore integration time:
-
Permalink:
lmmx/pydantic-function-models@8ca61797061643eba431730735538da9c7e300d0 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/lmmx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
CI.yml@8ca61797061643eba431730735538da9c7e300d0 -
Trigger Event:
workflow_run
-
Statement type: