duck.utils.callabletools

Module for callable utilities.

Module Contents

Functions

duplicate_callable

Creates a duplicate of the given callable (function or method) with the same signature.

get_callable_type

Return a descriptive type for the given callable.

API

duck.utils.callabletools.duplicate_callable(callable_obj, new_name=None, decorator=None)[source]

Creates a duplicate of the given callable (function or method) with the same signature.

This function is useful for dynamically generating new callables with the same signature as an existing callable, while optionally allowing the application of a decorator. The new callable can also be given a custom name.

Parameters:
  • callable_obj – The original callable to duplicate.

  • new_name – The name for the new duplicated callable. Defaults to None.

  • decorator – A decorator to apply to the callable. Defaults to None.

Returns:

A new callable with the same signature as the original callable.

Return type:

callable

Safety:

  • This function avoids using exec, which mitigates the risk of executing arbitrary code.

  • The function uses closures to ensure that the wrapper is correctly referenced, preventing name errors.

  • By preserving the callable’s signature and using functools.wraps, the callable’s metadata (like name and docstring) is maintained.

Example Usage:

# Define a function to be duplicated
def example_function(arg1, arg2, kwarg1=None, kwarg2=None):
    '''
    An example function that takes two positional arguments and two keyword arguments.
    '''
    return f"arg1: {arg1}, arg2: {arg2}, kwarg1: {kwarg1}, kwarg2: {kwarg2}"

# Create a duplicate of the function with a custom name
duplicated_function = duplicate_callable(example_function, new_name="duplicated_function")
result = duplicated_function(1, 2, kwarg1="test", kwarg2="example")
print(result)  # Output: arg1: 1, arg2: 2, kwarg1: test, kwarg2: example

# Define a class with a method to be duplicated
class MyClass:
    def example_method(self, arg1, arg2, kwarg1=None, kwarg2=None):
        '''
        An example method that takes two positional arguments and two keyword arguments.
        '''
        return f"arg1: {arg1}, arg2: {arg2}, kwarg1: {kwarg1}, kwarg2: {kwarg2}"

# Create a duplicate of the method with a custom name
MyClass.duplicated_method = duplicate_callable(MyClass.example_method, new_name="duplicated_method")
obj = MyClass()
result = obj.duplicated_method(1, 2, kwarg1="test", kwarg2="example")
print(result)  # Output: arg1: 1, arg2: 2, kwarg1: test, kwarg2: example
duck.utils.callabletools.get_callable_type(obj, owner: type = None) str[source]

Return a descriptive type for the given callable.

Parameters:
  • obj – Callable object to classify.

  • owner – Class owning the callable if known. Needed only to detect unbound methods.

Returns:

One of: - “bound_method” - “unbound_method” - “classmethod” - “staticmethod” - “function” - “callable_object” - “builtin_function” - “unknown”

Return type:

str