summaryrefslogtreecommitdiff
path: root/numpy/core/_internal.py
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2017-05-09 08:12:53 -0600
committerGitHub <noreply@github.com>2017-05-09 08:12:53 -0600
commitbeb4ae2b994d5315397211daab51431c611cce8c (patch)
tree3a9f7b47c1ea67662c5d999e7be27f142e6f15d7 /numpy/core/_internal.py
parentc11628abd820a1f44b052ea87af810f8f00cf2e4 (diff)
parent2e3202649692815e973f0e72820d25d5e536910d (diff)
downloadnumpy-beb4ae2b994d5315397211daab51431c611cce8c.tar.gz
Merge pull request #9026 from eric-wieser/ufunc_docstrings
ENH: Show full PEP 457 argument lists for ufuncs
Diffstat (limited to 'numpy/core/_internal.py')
-rw-r--r--numpy/core/_internal.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py
index f2f24bcc5..c890eba17 100644
--- a/numpy/core/_internal.py
+++ b/numpy/core/_internal.py
@@ -670,3 +670,48 @@ def array_ufunc_errmsg_formatter(ufunc, method, *inputs, **kwargs):
return ('operand type(s) do not implement __array_ufunc__'
'({!r}, {!r}, {}): {}'
.format(ufunc, method, args_string, types_string))
+
+def _ufunc_doc_signature_formatter(ufunc):
+ """
+ Builds a signature string which resembles PEP 457
+
+ This is used to construct the first line of the docstring
+ """
+
+ # input arguments are simple
+ if ufunc.nin == 1:
+ in_args = 'x'
+ else:
+ in_args = ', '.join('x{}'.format(i+1) for i in range(ufunc.nin))
+
+ # output arguments are both keyword or positional
+ if ufunc.nout == 0:
+ out_args = ', /, out=()'
+ elif ufunc.nout == 1:
+ out_args = ', /, out=None'
+ else:
+ out_args = '[, {positional}], / [, out={default}]'.format(
+ positional=', '.join(
+ 'out{}'.format(i+1) for i in range(ufunc.nout)),
+ default=repr((None,)*ufunc.nout)
+ )
+
+ # keyword only args depend on whether this is a gufunc
+ kwargs = (
+ ", casting='same_kind'"
+ ", order='K'"
+ ", dtype=None"
+ ", subok=True"
+ "[, signature"
+ ", extobj]"
+ )
+ if ufunc.signature is None:
+ kwargs = ", where=True" + kwargs
+
+ # join all the parts together
+ return '{name}({in_args}{out_args}, *{kwargs})'.format(
+ name=ufunc.__name__,
+ in_args=in_args,
+ out_args=out_args,
+ kwargs=kwargs
+ )