Skip to content

Method

Method

Method(method: Callable[..., RuntimeValue], cls: type, skip_self: bool = True)

Bases: Function

The Method class represents a method of a class.

Parameters:

Name Type Description Default
method Callable

The method to be inspected.

required
cls type

The class to which the method belongs.

required
skip_self bool

Whether to skip the self parameter.

True

Attributes:

Name Type Description
name str

The name of the method.

docstring str

The docstring of the method.

has_docstring bool

Whether the method has a docstring.

description str

The description part of the method's docstring.

params list[Parameter]

A list of parameters of the method.

dict dict

A dictionary representation of the method's attributes.

is_static bool

Whether the method is static.

is_classmethod bool

Whether the method is a classmethod.

is_property bool

Whether the method is a property.

is_private bool

Whether the method is private.

is_protected bool

Whether the method is protected.

is_public bool

Whether the method is public.

is_inherited bool

Whether the method is inherited.

Source code in objinspect/method.py
def __init__(
    self, method: Callable[..., RuntimeValue], cls: type, skip_self: bool = True
) -> None:
    super().__init__(method, skip_self)

    self.cls = cls

class_instance property

class_instance: RuntimeValue | None

The instance to which this method is bound, or None if unbound.

is_static property

is_static: bool

Whether the method is a static method.

is_classmethod property

is_classmethod: bool

Whether the method is a class method.

is_property property

is_property: bool

Whether the method is a property.

is_private property

is_private: bool

Whether the method is private (double underscore prefix, excluding dunders).

is_protected property

is_protected: bool

Whether the method is protected (single underscore prefix, excluding dunders).

is_public property

is_public: bool

Whether the method is public (no underscore prefix).

is_inherited property

is_inherited: bool

Whether the method is inherited from a parent class.

MethodFilter

MethodFilter(init: bool = True, public: bool = True, inherited: bool = True, static_methods: bool = True, protected: bool = False, private: bool = False, classmethod: bool = False)
Source code in objinspect/method.py
def __init__(
    self,
    init: bool = True,
    public: bool = True,
    inherited: bool = True,
    static_methods: bool = True,
    protected: bool = False,
    private: bool = False,
    classmethod: bool = False,
) -> None:
    self.checks: list[Callable[[Method], bool]] = []
    filter_checks: tuple[tuple[bool, Callable[[Method], bool]], ...] = (
        (not init, lambda method: method.name == "__init__"),
        (not static_methods, lambda method: method.is_static),
        (not inherited, lambda method: method.is_inherited),
        (not private, lambda method: method.is_private),
        (not protected, lambda method: method.is_protected),
        (not public, lambda method: method.is_public),
        (not classmethod, lambda method: method.is_classmethod),
    )
    for enabled, check in filter_checks:
        if enabled:
            self.checks.append(check)

check

check(method: Method) -> bool

Check if a method passes all filter criteria.

Parameters:

Name Type Description Default
method Method

The method to check.

required

Returns:

Name Type Description
bool bool

True if the method passes all filters, False otherwise.

Source code in objinspect/method.py
def check(self, method: Method) -> bool:
    """
    Check if a method passes all filter criteria.

    Args:
        method (Method): The method to check.

    Returns:
        bool: True if the method passes all filters, False otherwise.
    """
    return all(not check_func(method) for check_func in self.checks)

extract

extract(methods: list[Method]) -> list[Method]

Filter a list of methods based on the configured criteria.

Parameters:

Name Type Description Default
methods list[Method]

The list of methods to filter.

required

Returns:

Type Description
list[Method]

list[Method]: The filtered list of methods.

Source code in objinspect/method.py
def extract(self, methods: list[Method]) -> list[Method]:
    """
    Filter a list of methods based on the configured criteria.

    Args:
        methods (list[Method]): The list of methods to filter.

    Returns:
        list[Method]: The filtered list of methods.
    """
    return [i for i in methods if self.check(i)]

split_args_kwargs

split_args_kwargs(func_args: dict[str, RuntimeValue], func: Function | Method) -> tuple[tuple[RuntimeValue, ...], dict[str, RuntimeValue]]

Split the arguments passed to a function into positional and keyword arguments.

Source code in objinspect/method.py
def split_args_kwargs(
    func_args: dict[str, RuntimeValue],
    func: Function | Method,
) -> tuple[tuple[RuntimeValue, ...], dict[str, RuntimeValue]]:
    """Split the arguments passed to a function into positional and keyword arguments."""
    args: list[RuntimeValue] = []
    kwargs: dict[str, RuntimeValue] = {}
    for param in func.params:
        if param.kind == _ParameterKind.POSITIONAL_ONLY:
            args.append(func_args[param.name])
        else:
            kwargs[param.name] = func_args[param.name]

    return tuple(args), kwargs