Skip to content

Better introspection support for decorated functions #899

@skirpichev

Description

@skirpichev

Currently we have something like this:

>>> from mpmath import *
>>> import inspect
>>> help(polylog)
Help on method f_wrapped in module mpmath.ctx_mp_python:

f_wrapped(*args, **kwargs) method of mpmath.ctx_mp.MPContext instance
    Computes the polylogarithm, defined by the sum

    .. math ::

        \mathrm{Li}_s(z) = \sum_{k=1}^{\infty} \frac{z^k}{k^s}.

    This series is convergent only for `|z| < 1`, so elsewhere
    the analytic continuation is implied.

    The polylogarithm should not be confused with the logarithmic
    integral (also denoted by Li or li), which is implemented
    as :func:`~mpmath.li`.
[...]
>>> inspect.signature(polylog)
<Signature (*args, **kwargs)>

You can only guess in which order arguments appear in the function (sometimes it's very important, as there are different conventions in literature). The f_wrapped name also doesn't help anyone.

A quick patch:

diff --git a/mpmath/ctx_mp_python.py b/mpmath/ctx_mp_python.py
index 1c11d17..e406f01 100644
--- a/mpmath/ctx_mp_python.py
+++ b/mpmath/ctx_mp_python.py
@@ -1108,6 +1108,9 @@ def f_wrapped(ctx, *args, **kwargs):
         else:
             f_wrapped = f
         f_wrapped.__doc__ = function_docs.__dict__.get(name, f.__doc__)
+        import inspect
+        f_wrapped.__signature__ = inspect.signature(f)
+        f_wrapped.__name__ = f.__name__
         setattr(cls, name, f_wrapped)
 
     def _convert_param(ctx, x):

make things slightly better:

>>> help(polylog)
Help on method polylog in module mpmath.ctx_mp_python:

polylog(s, z) method of mpmath.ctx_mp.MPContext instance
    Computes the polylogarithm, defined by the sum

    .. math ::

        \mathrm{Li}_s(z) = \sum_{k=1}^{\infty} \frac{z^k}{k^s}.
[...]
>>> inspect.signature(polylog)
<Signature (s, z)>

I'm not sure that we should make arguments names to be a part of API, however:

>>> polylog(2, -3)
mpf('-1.9393754207667089')
>>> polylog(2, z=-3)
mpf('-1.9393754207667089')
>>> polylog(s=2, z=-3)
mpf('-1.9393754207667089')

but there is not simple ways to avoid this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsenhancementnew feature requests (or implementation)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions