Skip to content

Function

Function

Function(func: Callable, 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 str

The docstring of the function.

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, skip_self: bool = True) -> None:
    self.func = func
    self.skip_self = skip_self
    self.name: str = self.func.__name__
    self.docstring = inspect.getdoc(self.func)
    self.has_docstring = _has_docstr(self.docstring)
    self._parsed_docstr: Docstring | None = (
        docstring_parser.parse(self.docstring) if self.has_docstring else None  # type: ignore
    )
    self.return_type = NoneType
    self._parameters = self._get_parameters()
    self.description = _get_docstr_desc(self._parsed_docstr)

params property

params: list[Parameter]

Returns a list of parameters of the 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, **kwargs) -> Any

Calls the function and returns the result of its call.

Parameters:

Name Type Description Default
*args

Positional arguments passed to the function.

()
**kwargs

Keyword arguments passed to the function.

{}
Source code in objinspect/function.py
def call(self, *args, **kwargs) -> Any:
    """
    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)

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 = self.name if not color else colored(self.name, theme.name)
    params = ", ".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:
        return_str = type_name(self.return_type)
        if color:
            return_str = colored(return_str, theme.ret)
        return_str = " -> " + return_str
    else:
        return_str = ""

    string = 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)
        return string + description_str
    return string