Skip to content

util

call_method

call_method(obj: object, name: str, args: tuple = (), kwargs: dict | None = None) -> Any

Call a method with the given name on the given object.

Parameters:

Name Type Description Default
obj object

The object to call the method on.

required
name str

The name of the method to call.

required
args tuple

The positional arguments to pass to the method.

()
kwargs dict

The keyword arguments to pass to the method.

None

Returns:

Name Type Description
object Any

The result of calling the method.

Example
>>> import math
>>> call_method(math, "pow", args=(2, 2))
4.0
Source code in objinspect/util.py
def call_method(obj: object, name: str, args: tuple = (), kwargs: dict | None = None) -> Any:
    """
    Call a method with the given name on the given object.

    Args:
        obj (object): The object to call the method on.
        name (str): The name of the method to call.
        args (tuple, optional): The positional arguments to pass to the method.
        kwargs (dict, optional): The keyword arguments to pass to the method.

    Returns:
        object: The result of calling the method.

    Example:
        ```python
        >>> import math
        >>> call_method(math, "pow", args=(2, 2))
        4.0
        ```
    """
    kwargs = kwargs or {}
    return getattr(obj, name)(*args, **kwargs)

get_uninherited_methods

get_uninherited_methods(cls) -> list[str]

Get the methods of a class that are not inherited from its parent classes.

Source code in objinspect/util.py
def get_uninherited_methods(cls) -> list[str]:
    """Get the methods of a class that are not inherited from its parent classes."""
    return [
        name
        for name, method in cls.__dict__.items()
        if isinstance(method, (FunctionType, classmethod, staticmethod))
    ]

create_function

create_function(name: str, args: dict[str, tuple[Any, Any]], body: str | list[str], globs: dict[str, Any], return_type: Any | EMPTY = EMPTY, docstring: str | None = None) -> Callable[..., Any]

Create a function with the given name, arguments, body, and globals.

Parameters:

Name Type Description Default
name str

The name of the function.

required
args dict

A dictionary mapping argument names to tuples of the argument type and default value.

required
body str | list

The body of the function. If a string, it will be split by newlines.

required
globs dict

The globals to use when executing the function.

required
return_type Any

The return type of the function.

EMPTY
docstring str

The docstring of the function.

None
Example
>>> add = create_function(
...     name="add",
...     args={
...         "a": (int, None),
...         "b": (int, 2),
...     },
...     body=[
...         "result = a + b",
...         "return result",
...          ],
...     docstring="Adds two numbers together. If b is not provided, defaults to 2.",
...     globs=globals(),
...   )
>>> add(2, 2)
4
Source code in objinspect/util.py
def create_function(
    name: str,
    args: dict[str, tuple[Any, Any]],
    body: str | list[str],
    globs: dict[str, Any],
    return_type: Any | EMPTY = EMPTY,
    docstring: str | None = None,
) -> Callable[..., Any]:
    """
    Create a function with the given name, arguments, body, and globals.

    Args:
        name (str): The name of the function.
        args (dict): A dictionary mapping argument names to tuples of the argument type and default value.
        body (str | list): The body of the function. If a string, it will be split by newlines.
        globs (dict): The globals to use when executing the function.
        return_type (Any, optional): The return type of the function.
        docstring (str, optional): The docstring of the function.

    Example:
        ```python
        >>> add = create_function(
        ...     name="add",
        ...     args={
        ...         "a": (int, None),
        ...         "b": (int, 2),
        ...     },
        ...     body=[
        ...         "result = a + b",
        ...         "return result",
        ...          ],
        ...     docstring="Adds two numbers together. If b is not provided, defaults to 2.",
        ...     globs=globals(),
        ...   )
        >>> add(2, 2)
        4
        ```
    """
    arg_str = []
    for arg, annotations in args.items():
        if len(annotations) == 2:
            t, default = annotations
        elif len(annotations) == 1:
            t = annotations[0]
            default = EMPTY
        else:
            raise ValueError(f"Invalid annotations for argument {arg}: {annotations}")

        if t is not EMPTY:
            arg_str.append(f"{arg}: {t.__name__}")
        else:
            arg_str.append(arg)
        if default is not EMPTY:
            arg_str[-1] += f" = {repr(default)}"

    arg_str = ", ".join(arg_str)
    body_str = "\n    ".join(body) if isinstance(body, list) else body
    func_str = f"def {name}({arg_str})"

    if return_type is not EMPTY:
        if return_type is None:
            func_str += " -> None"
        else:
            func_str += f" -> {return_type.__name__}"

    func_str += ":"
    if docstring:
        func_str += f'\n    """{docstring}"""'
    func_str += f"\n    {body_str}"

    code_obj = compile(func_str, "<string>", "exec")
    exec(code_obj, globs)
    func = globs[name]
    func.__annotations__ = {arg: annotation[0] for arg, annotation in args.items()}
    if return_type is not EMPTY:
        func.__annotations__["return"] = return_type

    return func