You have (at least) two alternatives to achieve what you want.
The best alternative would be to override the __str__
method of the inspect.Signature
class. However, as it is written in C, it is read only.
So to do that you need to extend the class as following:
class MySignature(inspect.Signature):
def __str__(self):
return '(...)'
then after defining your function you execute:
func_signature = inspect.signature(func)
func.__signature__ = MySignature(func_signature.parameters.values(),
return_annotation=func_signature.return_annotation)
which would then return the following for help(func)
:
Help on function func in module __main__:
func(...)
(END)
With this approach inspect.signature still works:
In [1]: inspect.signature(func)
Out[1]: <MySignature (...)>
Alternatively if you don't really care about being able to properly introspect your function (and probably some other use cases), then you can define the value of your function's __signature__
to an object which is not a Signature
instance:
def func(x, y):
pass
func.__signature__ = object()
help(func)
generates the result:
Help on function func in module __main__:
func(...)
(END)
But now inspect.signature(func)
will raise TypeError: unexpected object <object object at 0x10182e200> in __signature__ attribute
.
Note: this last version is quite hacky and I would not recommend it.
For more info on these two techniques and how the signature works see PEP 0362.
Update:
For python 2.7 you can do the following (probably better using a mock framework):
In [1]: import inspect
In [2]: def myformatargspec(*args, **kwargs):
...: return '(...)'
...:
In [3]: def func(x, y):
...: pass
...:
In [6]: inspect.formatargspec = myformatargspec
In [7]: help(func)
Help on function func in module __main__:
func(...)
(END)