pydantic-function-models


Namepydantic-function-models JSON
Version 0.1.10 PyPI version JSON
download
home_pageNone
SummaryMigrating v1 Pydantic ValidatedFunction to v2.
upload_time2025-02-17 16:53:34
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT
keywords pydantic serialization deserialization parsing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pydantic Function Models

[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
[![pdm-managed](https://img.shields.io/badge/pdm-managed-blueviolet)](https://pdm.fming.dev)
[![PyPI](https://img.shields.io/pypi/v/pydantic-function-models.svg)](https://pypi.org/project/pydantic-function-models)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/pydantic-function-models.svg)](https://pypi.org/project/pydantic-function-models)
[![License](https://img.shields.io/pypi/l/pydantic-function-models.svg)](https://pypi.python.org/pypi/pydantic-function-models)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/lmmx/pydantic-function-models/master.svg)](https://results.pre-commit.ci/latest/github/lmmx/pydantic-function-models/master)

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

```bash
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](https://docs.pydantic.dev/latest/api/validate_call/)
- **Painless migration from Pydantic v1**: Provides a `ValidatedFunction` class ported to v2, for those who relied on `ValidatedFunction` in v1.
- **Clear error messages**: Raises Pydantic `ValidationError` with 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

```python
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 for `ValidatedFunction`, 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](https://github.com/lmmx). Contributions are welcome!

1. **Issues & Discussions**: Please open a GitHub issue or discussion for bugs, feature requests, or questions.
2. **Pull Requests**: PRs are welcome!
   - Install the dev extra (e.g. with [uv](https://docs.astral.sh/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.

## License

This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pydantic-function-models",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "pydantic, serialization, deserialization, parsing",
    "author": null,
    "author_email": "Louis Maddox <louismmx@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/95/83/dc9cf4c16159266e643a16b14dd90c24e859670fbe2611140c0cd5503cae/pydantic_function_models-0.1.10.tar.gz",
    "platform": null,
    "description": "# Pydantic Function Models\n\n[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)\n[![pdm-managed](https://img.shields.io/badge/pdm-managed-blueviolet)](https://pdm.fming.dev)\n[![PyPI](https://img.shields.io/pypi/v/pydantic-function-models.svg)](https://pypi.org/project/pydantic-function-models)\n[![Supported Python versions](https://img.shields.io/pypi/pyversions/pydantic-function-models.svg)](https://pypi.org/project/pydantic-function-models)\n[![License](https://img.shields.io/pypi/l/pydantic-function-models.svg)](https://pypi.python.org/pypi/pydantic-function-models)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/lmmx/pydantic-function-models/master.svg)](https://results.pre-commit.ci/latest/github/lmmx/pydantic-function-models/master)\n\nA 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.\n\n## Installation\n\n```bash\npip install pydantic-function-models\n```\n\n## Features\n\n- **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](https://docs.pydantic.dev/latest/api/validate_call/)\n- **Painless migration from Pydantic v1**: Provides a `ValidatedFunction` class ported to v2, for those who relied on `ValidatedFunction` in v1.\n- **Clear error messages**: Raises Pydantic `ValidationError` with helpful details if input arguments do not match the declared types.\n- **Reserved name checks**: Prevents usage of special parameter names (e.g., `v__args`, `v__kwargs`) that could conflict with internal validation logic.\n\n## Usage\n\nBelow 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.\n\n### Command-Line Interface\n\nThis library does **not** provide a CLI. All functionality is accessed by importing and using its classes in Python code. The remaining \u201cExamples\u201d section demonstrates typical usage patterns.\n\n#### Example: Basic Validated Function\n\n```python\nfrom pydantic_function_models import ValidatedFunction\n\ndef add(a: int, b: int) -> int:\n    return a + b\n\nvf = ValidatedFunction(add)\n\n# Build arguments for validation\nargs_to_validate = (1,)\nkwargs_to_validate = {\"b\": 2}\n\nvalidated = vf.model.model_validate({\n    \"a\": args_to_validate[0],\n    \"b\": kwargs_to_validate[\"b\"]\n})\nresult = add(**validated.model_dump(exclude_unset=True))\n\nprint(result)  # 3\n```\n\nHere, `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`.\n\n### Library Usage\n\nInternally, each `ValidatedFunction` builds a Pydantic model representing the function\u2019s signature. When you call `.model.model_validate(...)` or `.build_values(...)`, it validates the provided arguments (whether positional or keyword-based) against the signature.\n\n---\n\n## Project Structure\n\nTake a look at the internals here:\n\n- `validated_function.py`: Core logic for `ValidatedFunction`, which encapsulates argument validation and Pydantic model generation.\n- `parameters.py`: Model definitions for function parameters/signatures, with checks for reserved argument names.\n\n## Contributing\n\nMaintained by [lmmx](https://github.com/lmmx). Contributions are welcome!\n\n1. **Issues & Discussions**: Please open a GitHub issue or discussion for bugs, feature requests, or questions.\n2. **Pull Requests**: PRs are welcome!\n   - Install the dev extra (e.g. with [uv](https://docs.astral.sh/uv/): `uv pip install -e .[dev]`)\n   - Run tests (e.g. `pytest`) and include updates to docs or examples if relevant.\n   - If reporting a bug, please include the version and error message/traceback if available.\n\n## License\n\nThis project is licensed under the [MIT License](https://opensource.org/licenses/MIT).\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Migrating v1 Pydantic ValidatedFunction to v2.",
    "version": "0.1.10",
    "project_urls": null,
    "split_keywords": [
        "pydantic",
        " serialization",
        " deserialization",
        " parsing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0bc6c8412c88f4113b7cf3b33ae08f4abcd94fe0413d09ec49780f35f8f9e790",
                "md5": "6ccadfe787adde66a3b98bd3e8d95989",
                "sha256": "9c1b0be9537a48f3ad9e3d9dd6c4e9ebcce98dd79a1bb329868b576cf50452c1"
            },
            "downloads": -1,
            "filename": "pydantic_function_models-0.1.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6ccadfe787adde66a3b98bd3e8d95989",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 8061,
            "upload_time": "2025-02-17T16:53:28",
            "upload_time_iso_8601": "2025-02-17T16:53:28.904707Z",
            "url": "https://files.pythonhosted.org/packages/0b/c6/c8412c88f4113b7cf3b33ae08f4abcd94fe0413d09ec49780f35f8f9e790/pydantic_function_models-0.1.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9583dc9cf4c16159266e643a16b14dd90c24e859670fbe2611140c0cd5503cae",
                "md5": "b8e4eb699770751feaa85de4f3eca1b3",
                "sha256": "d88e37c19bc2b9d88850a6f00f0227212aae1b0d55de45c9de7af65373844027"
            },
            "downloads": -1,
            "filename": "pydantic_function_models-0.1.10.tar.gz",
            "has_sig": false,
            "md5_digest": "b8e4eb699770751feaa85de4f3eca1b3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 9150,
            "upload_time": "2025-02-17T16:53:34",
            "upload_time_iso_8601": "2025-02-17T16:53:34.769428Z",
            "url": "https://files.pythonhosted.org/packages/95/83/dc9cf4c16159266e643a16b14dd90c24e859670fbe2611140c0cd5503cae/pydantic_function_models-0.1.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-17 16:53:34",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "pydantic-function-models"
}
        
Elapsed time: 0.38369s