Skip to content

BoundFunction

Base classes

Name Children Inherits
Generic
typing
Abstract base class for generic types.

⋔ Inheritance diagram

graph TD
  2506339856["bind_kwargs.BoundFunction"]
  3481597255["typing.Generic"]
  2751680898["builtins.object"]
  3481597255 --> 2506339856
  2751680898 --> 3481597255

🛈 DocStrings

A function with pre-bound parameters.

This class wraps a function and binds some parameters to fixed values, while updating the signature and docstring to reflect only the remaining parameters that can still be provided when calling.

Source code in src/schemez/bind_kwargs.py
class BoundFunction[T]:
    """A function with pre-bound parameters.

    This class wraps a function and binds some parameters to fixed values,
    while updating the signature and docstring to reflect only the remaining
    parameters that can still be provided when calling.
    """

    def __init__(self, func: Callable[..., T], **bound_kwargs: Any):
        """Initialize with a function and parameters to bind.

        Args:
            func: The function to wrap
            **bound_kwargs: Parameters to bind to fixed values

        Raises:
            ValueError: If any parameter name is not in the function signature
        """
        self.func = func
        self.bound_kwargs = bound_kwargs
        self.__name__ = get_object_name(func, "unknown")
        self.__module__ = func.__module__
        self.__qualname__ = get_object_qualname(func, "unknown")
        self.__doc__ = remove_kwargs_from_docstring(func.__doc__, self.bound_kwargs)
        self.__annotations__ = self._update_annotations(_get_annotations(func))
        self.__signature__ = self._update_signature()

        # Verify all bound kwargs are valid parameters
        sig = get_signature(func)
        for param in bound_kwargs:
            if param not in sig.parameters:
                name = get_object_name(func, "unknown")
                msg = f"Parameter {param!r} not found in signature of {name}"
                raise ValueError(msg)

    def __call__(self, *args: Any, **kwargs: Any) -> T:
        """Call the function with the bound parameters.

        Args:
            *args: Positional arguments for the function
            **kwargs: Keyword arguments for the function

        Returns:
            The return value from the wrapped function
        """
        # Combine bound parameters with provided parameters
        all_kwargs = {**self.bound_kwargs, **kwargs}
        return self.func(*args, **all_kwargs)

    def _update_signature(self) -> inspect.Signature:
        """Create a new signature excluding bound parameters.

        Returns:
            Updated signature without bound parameters
        """
        sig = get_signature(self.func)
        params = [p for name, p in sig.parameters.items() if name not in self.bound_kwargs]
        return sig.replace(parameters=params)

    def _update_annotations(self, annotations: dict[str, Any]) -> dict[str, Any]:
        """Remove bound parameters from annotations.

        Args:
            annotations: Original function annotations

        Returns:
            Updated annotations dictionary
        """
        return {
            name: ann
            for name, ann in annotations.items()
            if name not in self.bound_kwargs and name != "return"
        }

__call__

__call__(*args: Any, **kwargs: Any) -> T

Call the function with the bound parameters.

Parameters:

Name Type Description Default
*args Any

Positional arguments for the function

()
**kwargs Any

Keyword arguments for the function

{}

Returns:

Type Description
T

The return value from the wrapped function

Source code in src/schemez/bind_kwargs.py
def __call__(self, *args: Any, **kwargs: Any) -> T:
    """Call the function with the bound parameters.

    Args:
        *args: Positional arguments for the function
        **kwargs: Keyword arguments for the function

    Returns:
        The return value from the wrapped function
    """
    # Combine bound parameters with provided parameters
    all_kwargs = {**self.bound_kwargs, **kwargs}
    return self.func(*args, **all_kwargs)

__init__

__init__(func: Callable[..., T], **bound_kwargs: Any)

Initialize with a function and parameters to bind.

Parameters:

Name Type Description Default
func Callable[..., T]

The function to wrap

required
**bound_kwargs Any

Parameters to bind to fixed values

{}

Raises:

Type Description
ValueError

If any parameter name is not in the function signature

Source code in src/schemez/bind_kwargs.py
def __init__(self, func: Callable[..., T], **bound_kwargs: Any):
    """Initialize with a function and parameters to bind.

    Args:
        func: The function to wrap
        **bound_kwargs: Parameters to bind to fixed values

    Raises:
        ValueError: If any parameter name is not in the function signature
    """
    self.func = func
    self.bound_kwargs = bound_kwargs
    self.__name__ = get_object_name(func, "unknown")
    self.__module__ = func.__module__
    self.__qualname__ = get_object_qualname(func, "unknown")
    self.__doc__ = remove_kwargs_from_docstring(func.__doc__, self.bound_kwargs)
    self.__annotations__ = self._update_annotations(_get_annotations(func))
    self.__signature__ = self._update_signature()

    # Verify all bound kwargs are valid parameters
    sig = get_signature(func)
    for param in bound_kwargs:
        if param not in sig.parameters:
            name = get_object_name(func, "unknown")
            msg = f"Parameter {param!r} not found in signature of {name}"
            raise ValueError(msg)

:fa-brands-github: Show source on GitHub