You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH: Make inspect.signature work for numpy C functions #8734
I'm currently working on a POC, not sure if it's possible the way numpy does it right now. I'm not sure at which stage the __text_signature__ thing intercepts the documentation parsing.
@pv The signature lines (including the --after the signature) are removed from the "docstring" in this process. Numpydoc can (at least since numpy/numpydoc#73) correctly recognize that signature as well.
--- a/numpy/lib/function_base.py+++ b/numpy/lib/function_base.py@@ -4511,7 +4511,7 @@ def trapz(y, x=None, dx=1.0, axis=-1):
#always succeed
-def add_newdoc(place, obj, doc):+def add_newdoc(place, obj, doc, sig=None):
"""
Adds documentation to obj which is in module place.
@@ -4532,15 +4532,23 @@ def add_newdoc(place, obj, doc):
routine never raises an error the caller must check manually
that the docstrings were changed.
"""
+ def _add_doc(obj, sig_or_doc, doc=None):+ if doc is not None:+ doc = ' ' + sig_or_doc+ '\n--\n\n' + doc+ else:+ doc = sig_or_doc+ doc = doc.strip()+ add_docstring(obj, doc)+
try:
new = getattr(__import__(place, globals(), {}, [obj]), obj)
if isinstance(doc, str):
- add_docstring(new, doc.strip())+ _add_doc(new, doc, sig)
elif isinstance(doc, tuple):
- add_docstring(getattr(new, doc[0]), doc[1].strip())+ _add_doc(getattr(new, doc[0]), *doc[1:])
elif isinstance(doc, list):
for val in doc:
- add_docstring(getattr(new, val[0]), val[1].strip())+ _add_doc(getattr(new, val[0]), *val[1:])
except:
pass
a regex replacing """\n ([\w.]+\(.*\))\n\n with "\1",\n """\n in add_newdocs.py, and a couple fixes of single/double quotes does the trick for populating __text_signature__.
Unfortunately, there are a lot of cases where this causes help(func) to throw a RuntimeError when it fails to parse a somewhat reasonable signature. Perhaps there's a patch for upstream python there
Oops, you're right. I wasn't aware that numpy did string conversion of dtypes like that. Either way, I don't think we want to encourage passing 'float' over float, which is what the effect would be
Activity
MSeifert04 commentedon Mar 3, 2017
I'm currently working on a POC, not sure if it's possible the way numpy does it right now. I'm not sure at which stage the
__text_signature__thing intercepts the documentation parsing.pv commentedon Mar 3, 2017
numpydoc convention is to put the signature first, without the
--.MSeifert04 commentedon Mar 3, 2017
@pv The signature lines (including the
--after the signature) are removed from the "docstring" in this process. Numpydoc can (at least since numpy/numpydoc#73) correctly recognize that signature as well.eric-wieser commentedon Mar 3, 2017
Frustratingly, python can't deal with a signature like
func(dtype=float), due to these linespv commentedon Mar 3, 2017
I see, this is something supported by Python function objects or types.
That's fine then as far as numpydoc is concerned.
eric-wieser commentedon Mar 3, 2017
Small patch to
function_base.pyofa regex replacing
"""\n ([\w.]+\(.*\))\n\nwith"\1",\n """\ninadd_newdocs.py, and a couple fixes of single/double quotes does the trick for populating__text_signature__.Unfortunately, there are a lot of cases where this causes
help(func)to throw aRuntimeErrorwhen it fails to parse a somewhat reasonable signature. Perhaps there's a patch for upstream python thereMSeifert04 commentedon Mar 3, 2017
@eric-wieser That diff is a really nice way to do it. That would also allow to not add the
\n--\n\nin Python < 3.4.My proof of concept (https://github.com/MSeifert04/numpy/tree/signature_thing) was more manually, I just modified the
ndarray.alldocstring to see if it worked:Regarding the dtype
RuntimeError: Just usedtype='float'in the signature?eric-wieser commentedon Mar 3, 2017
I think having incorrect signatures like 'float' is worse than none at all. We could just check if the signature parses correctly before assigning it
MSeifert04 commentedon Mar 3, 2017
Why is "float" incorrect?
eric-wieser commentedon Mar 3, 2017
Oops, you're right. I wasn't aware that numpy did string conversion of dtypes like that. Either way, I don't think we want to encourage passing
'float'overfloat, which is what the effect would beMSeifert04 commentedon Mar 3, 2017
@eric-wieser I may be wrong but
floatisn't correct either because the correct default isnp.float_?eric-wieser commentedon Apr 20, 2019
@MSeifert: you're right, that would be best of all.
I wonder if the array_function wrappers have made this change less worthwhile...
saulshanabrook commentedon May 26, 2020
It seems like you can also add a
__signature__property on the function to actually provide the Signature instance?This might give a bit more flexibility than the
__text_signature__.eric-wieser commentedon May 26, 2020
I don't believe it's possible to attach extra properties to C extension methods, although I remember seeing a PEP about it.