Skip to content

Function

Function

Function(func: Callable[..., RuntimeValue], skip_self: bool = True)

A Function object represents a function and its attributes.

Parameters:

Name Type Description Default
func Callable

The function to be inspected.

required
skip_self bool

Whether to skip the self parameter.

True

Attributes:

Name Type Description
name str

The name of the function.

docstring Docstring | None

The parsed docstring object.

docstring_text str | None

The raw docstring text.

has_docstring bool

Whether the function has a docstring.

description str

The description part of the function's docstring.

params list[Parameter]

A list of parameters of the function.

dict dict

A dictionary representation of the function's attributes.

Source code in objinspect/function.py
def __init__(self, func: Callable[..., RuntimeValue], skip_self: bool = True) -> None:
    self.func = func
    self.skip_self = skip_self
    self.name: str = self.func.__name__
    self.docstring_text: str | None = inspect.getdoc(self.func)
    self.has_docstring = has_docstr(self.docstring_text)
    if self.has_docstring and self.docstring_text is not None:
        self.docstring: Docstring | None = docstring_parser.parse(self.docstring_text)
    else:
        self.docstring = None

    self.return_type = NoneType
    self._parameters = self._get_parameters()
    self.description = get_docstr_description(self.docstring)

params property

params: list[Parameter]

Returns a list of parameters of the function.

dict property

dict: dict[str, object]

Return a dictionary representation of the function.

is_coroutine property

is_coroutine: bool

Whether the function is a coroutine (async function).

get_param

get_param(arg: str | int) -> Parameter

Retrieve a single Parameter object.

Parameters:

Name Type Description Default
arg str | int

Either the name or index of the parameter to retrieve.

required

Returns:

Name Type Description
Parameter Parameter

A Parameter object.

Raises:

Type Description
TypeError

If arg is not a string or an integer.

Source code in objinspect/function.py
def get_param(self, arg: str | int) -> Parameter:
    """
    Retrieve a single `Parameter` object.

    Args:
        arg (str | int): Either the name or index of the parameter to retrieve.

    Returns:
        Parameter: A `Parameter` object.

    Raises:
        TypeError: If arg is not a string or an integer.
    """
    match arg:
        case str():
            return self._parameters[arg]
        case int():
            return self.params[arg]
        case _:
            raise TypeError(type(arg))

call

call(*args: RuntimeValue, **kwargs: RuntimeValue) -> RuntimeValue

Calls the function and returns the result of its call.

Parameters:

Name Type Description Default
*args RuntimeValue

Positional arguments passed to the function.

()
**kwargs RuntimeValue

Keyword arguments passed to the function.

{}
Source code in objinspect/function.py
def call(self, *args: RuntimeValue, **kwargs: RuntimeValue) -> RuntimeValue:
    """
    Calls the function and returns the result of its call.

    Args:
        *args: Positional arguments passed to the function.
        **kwargs: Keyword arguments passed to the function.
    """
    return self.func(*args, **kwargs)

call_async async

call_async(*args: RuntimeValue, **kwargs: RuntimeValue) -> RuntimeValue

Call the function and await its result when needed.

Parameters:

Name Type Description Default
*args RuntimeValue

Positional arguments passed to the function.

()
**kwargs RuntimeValue

Keyword arguments passed to the function.

{}
Source code in objinspect/function.py
async def call_async(self, *args: RuntimeValue, **kwargs: RuntimeValue) -> RuntimeValue:
    """
    Call the function and await its result when needed.

    Args:
        *args: Positional arguments passed to the function.
        **kwargs: Keyword arguments passed to the function.
    """
    result = self.func(*args, **kwargs)
    if inspect.isawaitable(result):
        return await result

    return result

as_str

as_str(*, color: bool = True, description: bool = True, ljust: int = 58, theme: FunctionStrTheme | None = None) -> str

Return a string representation of the function.

Parameters:

Name Type Description Default
color bool

Whether to colorize the string.

True
description bool

Whether to include the description of the function.

True
ljust int

The width of the string.

58
theme FunctionStrTheme

Color theme to use. Default will be used if None.

None
Source code in objinspect/function.py
def as_str(
    self,
    *,
    color: bool = True,
    description: bool = True,
    ljust: int = 58,
    theme: FunctionStrTheme | None = None,
) -> str:
    """
    Return a string representation of the function.

    Args:
        color (bool, optional): Whether to colorize the string.
        description (bool, optional): Whether to include the description of the function.
        ljust (int, optional): The width of the string.
        theme (FunctionStrTheme, optional): Color theme to use. Default will be used if None.

    """
    if theme is None:
        theme = FunctionStrTheme()

    name_str: str = self.name if not color else colored(self.name, theme.name)
    params: str = ", ".join([i.as_str(color=color) for i in self.params])
    if color:
        params = colored("(", theme.bracket) + params + colored(")", theme.bracket)

    if self.return_type is not EMPTY and self.return_type is not None:  # type: ignore[comparison-overlap]
        return_str: str = type_name(self.return_type)
        if color:
            return_str = colored(return_str, theme.ret)
        return_str = " -> " + return_str
    else:
        return_str = ""

    string: str = f"{name_str}{params}{return_str}"

    if description and self.description:
        string = ansi_ljust(string, ljust)
        description_str = f" # {self.description}"
        if color:
            description_str = colored(description_str, theme.description)

        string += description_str

        return string

    return string