Skip to content

NamespaceCallable

🛈 DocStrings

Wrapper for tool functions with proper repr and call interface.

Source code in src/schemez/code_generation/namespace_callable.py
@dataclass
class NamespaceCallable:
    """Wrapper for tool functions with proper repr and call interface."""

    callable: Callable[..., Any]
    """The callable function to execute."""

    name_override: str | None = None
    """Override name for the callable, defaults to callable.__name__."""

    def __post_init__(self) -> None:
        """Set function attributes for introspection."""
        self.__name__ = self.name_override or get_object_name(self.callable, "unknown")
        self.__doc__ = self.callable.__doc__ or ""

    @property
    def name(self) -> str:
        """Get the effective name of the callable."""
        return self.name_override or get_object_name(self.callable, "unknown")

    @classmethod
    def from_generator(cls, generator: ToolCodeGenerator) -> NamespaceCallable:
        """Create a NamespaceCallable from a ToolCodeGenerator.

        Args:
            generator: The generator to wrap

        Returns:
            NamespaceCallable instance
        """
        if generator.callable is None:
            msg = f"Callable required for NamespaceCallable {generator.name!r}. "
            raise ValueError(msg)
        return cls(generator.callable, generator.name_override)

    async def __call__(self, *args: Any, **kwargs: Any) -> Any:
        """Execute the wrapped callable.

        Results (also falsy ones) and exceptions pass through unchanged; only `None`
        becomes a short confirmation text.
        """
        result = self.callable(*args, **kwargs)
        if inspect.isawaitable(result):
            result = await result
        return "Operation completed successfully" if result is None else result

    def __repr__(self) -> str:
        """Return detailed representation for debugging."""
        return f"NamespaceCallable(name='{self.name}')"

    def __str__(self) -> str:
        """Return readable string representation."""
        return f"<tool: {self.name}>"

    @property
    def signature(self) -> str:
        """Get function signature for debugging."""
        try:
            sig = get_signature(self.callable)
        except ValueError, TypeError:
            return f"{self.name}(...)"
        else:
            return f"{self.name}{sig}"

callable instance-attribute

callable: Callable[..., Any]

The callable function to execute.

name property

name: str

Get the effective name of the callable.

name_override class-attribute instance-attribute

name_override: str | None = None

Override name for the callable, defaults to callable.name.

signature property

signature: str

Get function signature for debugging.

__call__ async

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

Execute the wrapped callable.

Results (also falsy ones) and exceptions pass through unchanged; only None becomes a short confirmation text.

Source code in src/schemez/code_generation/namespace_callable.py
async def __call__(self, *args: Any, **kwargs: Any) -> Any:
    """Execute the wrapped callable.

    Results (also falsy ones) and exceptions pass through unchanged; only `None`
    becomes a short confirmation text.
    """
    result = self.callable(*args, **kwargs)
    if inspect.isawaitable(result):
        result = await result
    return "Operation completed successfully" if result is None else result

__post_init__

__post_init__() -> None

Set function attributes for introspection.

Source code in src/schemez/code_generation/namespace_callable.py
def __post_init__(self) -> None:
    """Set function attributes for introspection."""
    self.__name__ = self.name_override or get_object_name(self.callable, "unknown")
    self.__doc__ = self.callable.__doc__ or ""

__repr__

__repr__() -> str

Return detailed representation for debugging.

Source code in src/schemez/code_generation/namespace_callable.py
def __repr__(self) -> str:
    """Return detailed representation for debugging."""
    return f"NamespaceCallable(name='{self.name}')"

__str__

__str__() -> str

Return readable string representation.

Source code in src/schemez/code_generation/namespace_callable.py
def __str__(self) -> str:
    """Return readable string representation."""
    return f"<tool: {self.name}>"

from_generator classmethod

from_generator(generator: ToolCodeGenerator) -> NamespaceCallable

Create a NamespaceCallable from a ToolCodeGenerator.

Parameters:

Name Type Description Default
generator ToolCodeGenerator

The generator to wrap

required

Returns:

Type Description
NamespaceCallable

NamespaceCallable instance

Source code in src/schemez/code_generation/namespace_callable.py
@classmethod
def from_generator(cls, generator: ToolCodeGenerator) -> NamespaceCallable:
    """Create a NamespaceCallable from a ToolCodeGenerator.

    Args:
        generator: The generator to wrap

    Returns:
        NamespaceCallable instance
    """
    if generator.callable is None:
        msg = f"Callable required for NamespaceCallable {generator.name!r}. "
        raise ValueError(msg)
    return cls(generator.callable, generator.name_override)

:fa-brands-github: Show source on GitHub