Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
arguments_schema.py45 linesDownload Raw Back to experimental
1"""Experimental module exposing a function to generate a core schema that validates callable arguments."""2 3from __future__ import annotations4 5from collections.abc import Callable6from typing import Any, Literal7 8from pydantic_core import CoreSchema9 10from pydantic import ConfigDict11from pydantic._internal import _config, _generate_schema, _namespace_utils12 13 14def generate_arguments_schema(15    func: Callable[..., Any],16    schema_type: Literal['arguments', 'arguments-v3'] = 'arguments-v3',17    parameters_callback: Callable[[int, str, Any], Literal['skip'] | None] | None = None,18    config: ConfigDict | None = None,19) -> CoreSchema:20    """Generate the schema for the arguments of a function.21 22    Args:23        func: The function to generate the schema for.24        schema_type: The type of schema to generate.25        parameters_callback: A callable that will be invoked for each parameter. The callback26            should take three required arguments: the index, the name and the type annotation27            (or [`Parameter.empty`][inspect.Parameter.empty] if not annotated) of the parameter.28            The callback can optionally return `'skip'`, so that the parameter gets excluded29            from the resulting schema.30        config: The configuration to use.31 32    Returns:33        The generated schema.34    """35    generate_schema = _generate_schema.GenerateSchema(36        _config.ConfigWrapper(config),37        ns_resolver=_namespace_utils.NsResolver(namespaces_tuple=_namespace_utils.ns_for_function(func)),38    )39 40    if schema_type == 'arguments':41        schema = generate_schema._arguments_schema(func, parameters_callback)  # pyright: ignore[reportArgumentType]42    else:43        schema = generate_schema._arguments_v3_schema(func, parameters_callback)  # pyright: ignore[reportArgumentType]44    return generate_schema.clean_schema(schema)45 
codekingpro/portable-devtools · Team Ai