M
M
mkone1122021-12-31 12:40:29
Python
mkone112, 2021-12-31 12:40:29

How to combine function signatures so that help displays multiple options?

There are several overloaded functions:

@singledispatch
def get():
    """main docstring"""
    ...

@get.register(str)
def _get_str(s):
    """str docstring"""
    print('str')\

@get.register(int)
def _get_int(i):
    print('int')

get(1)  # int
get('s')  # str

But help outputs only one signature:
help(get)
Help on function get in module __main__:

get()
    main docstring

I can copy the signature of one implementation to another:
get.__signature__ = inspect.signature(_get_str)
help(get)

Help on function get in module __main__:

get(s)
main docstring

But I would like to see all the signatures and docstrings in help at once, something like this:
Help on function get in module __main__:

get()
    main docstring
get(s)
    str docstring
get(i)

How can I do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mkone112, 2021-12-31
@mkone112

@singledispatch
def get(date):
    """main docstring"""
    print('main')

@get.register(datetime.date)
def _get_str(s):
    """str docstring"""
    print('date')

get.__doc__ = '\n'.join([
    get.__doc__,
    f'\b\b{get.__name__}{inspect.signature(_get_str)}',
    _get_str.__doc__,
])

This works, although there is a slight padding issue.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question