Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override default help message on python function

I have a function with many arguments and a detailed help message, e.g.:

def worker_function(arg1, arg2, arg3):
    """ desired help message:
    arg1 - blah
    arg2 - foo
    arg3 - bar
    """
    print arg1, arg2, arg3

I also have a wrapper function which does some accounting and then calls my worker_function, passing all arguments to it as is.

def wrapper_function(**args):
    """ this function calls worker_function """
    ### do something here ...
    worker_function(**args)

I want the help message (displayed by python built-in help() function) for wrapper function to have argument list and help message from worker function.

The closest solution I could get is to do:

wrapper_function.__doc__ += "\n\n" + worker_function.__doc__

This results in:

>>? help(wrapper_function)
Help on function wrapper_function in module __main__:

wrapper_function(**args)
    this function calls worker function

    desired help message:
       arg1 - blah
       arg2 - foo
       arg3 - bar

But this description is missing the essential part - the argument list, that is:

worker_function(arg1, arg2, arg3)

(In real-life function the argument list is long, with telling default values, and I would like that to be displayed automatically).

Is there a way to add an argument list or worker_function to the help message of wrapper_function?

like image 929
glexey Avatar asked Feb 15 '23 16:02

glexey


1 Answers

help() internally looks up the function data using the inspect module. Unless you overwrite the function meta data by default (if that’s even possible) I don’t think you can get rid of the wrapper function’s definition.

What you can do however is fill the wrapper function’s help text with information from the wrapped function. You can either use the inspect module yourself (especially the getfullargspec method), or you can just use the pydoc module (which is what help internally uses) to generate it for you.

import pydoc
def wrappedHelpText (wrappedFunc):
    def decorator (f):
         f.__doc__ = 'This method wraps the following method:\n\n' + pydoc.text.document(wrappedFunc)
         return f
    return decorator

@wrappedHelpText(worker_function)
def wrapper_function(**args):
    worker_function(**args)

Calling help on that will then generate a more useful output including the original signature.

>>> help(wrapper_function)
Help on function wrapper_function in module __main__:

wrapper_function(**args)
    This method wraps the following method:

    worker_function(arg1, arg2, arg3)
        desired help message:
        arg1 - blah
        arg2 - foo
        arg3 - bar
like image 57
poke Avatar answered Feb 17 '23 06:02

poke