8000 Merge pull request #9026 from eric-wieser/ufunc_docstrings · numpy/numpy@beb4ae2 · GitHub
[go: up one dir, main page]

Skip to content

Commit beb4ae2

Browse files
authored
Merge pull request #9026 from eric-wieser/ufunc_docstrings
ENH: Show full PEP 457 argument lists for ufuncs
2 parents c11628a + 2e32026 commit beb4ae2

File tree

6 files changed

+197
-201
lines changed

6 files changed

+197
-201
lines changed

doc/source/reference/ufuncs.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ them by defining certain special methods. For details, see
286286
:class:`ufunc`
287287
==============
288288

289+
.. _ufuncs.kwargs:
290+
289291
Optional keyword arguments
290292
--------------------------
291293

numpy/add_newdocs.py

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5421,40 +5421,19 @@ def luf(lamdaexpr, *args, **kwargs):
54215421
"""
54225422
Functions that operate element by element on whole arrays.
54235423
5424-
To see the documentation for a specific ufunc, use np.info(). For
5425-
example, np.info(np.sin). Because ufuncs are written in C
5424+
To see the documentation for a specific ufunc, use `info`. For
5425+
example, ``np.info(np.sin)``. Because ufuncs are written in C
54265426
(for speed) and linked into Python with NumPy's ufunc facility,
54275427
Python's help() function finds this page whenever help() is called
54285428
on a ufunc.
54295429
5430-
A detailed explanation of ufuncs can be found in the "ufuncs.rst"
5431-
file in the NumPy reference guide.
5430+
A detailed explanation of ufuncs can be found in the docs for :ref:`ufuncs`.
54325431
5433-
Unary ufuncs:
5434-
=============
5432+
Calling ufuncs:
5433+
===============
54355434
5436-
op(X, out=None)
5437-
Apply op to X elementwise
5438-
5439-
Parameters
5440-
----------
5441-
X : array_like
5442-
Input array.
5443-
out : array_like
5444-
An array to store the output. Must be the same shape as `X`.
5445-
5446-
Returns
5447-
-------
5448-
r : array_like
5449-
`r` will have the same shape as `X`; if out is provided, `r`
5450-
will be equal to out.
5451-
5452-
Binary ufuncs:
5453-
==============
5454-
5455-
op(X, Y, out=None)
5456-
Apply `op` to `X` and `Y` elementwise. May "broadcast" to make
5457-
the shapes of `X` and `Y` congruent.
5435+
op(*x[, out], where=True, **kwargs)
5436+
Apply `op` to the arguments `*x` elementwise, broadcasting the arguments.
54585437
54595438
The broadcasting rules are:
54605439
@@ -5463,18 +5442,23 @@ def luf(lamdaexpr, *args, **kwargs):
54635442
54645443
Parameters
54655444
----------
5466-
X : array_like
5467-
First input array.
5468-
Y : array_like
5469-
Second input array.
5470-
out : array_like
5471-
An array to store the output. Must be the same shape as the
5472-
output would have.
5445+
*x : array_like
5446+
Input arrays.
5447+
out : ndarray or tuple of ndarray, optional
5448+
Alternate array object(s) in which to put the result; if provided, it
5449+
must have a shape that the inputs broadcast to.
5450+
where : array_like, optional
5451+
Values of True indicate to calculate the ufunc at that position, values
5452+
of False indicate to leave the value in the output alone.
5453+
**kwargs
5454+
For other keyword-only arguments, see the :ref:`ufunc docs <ufuncs.kwargs>`.
54735455
54745456
Returns
54755457
-------
5476-
r : array_like
5477-
The return value; if out is provided, `r` will be equal to out.
5458+
r : ndarray or tuple of ndarray
5459+
`r` will have the shape that the arrays in `x` broadcast to; if `out` is
5460+
provided, `r` will be equal to `out`. If the function has more than one
5461+
output, then the result will be a tuple of arrays.
54785462
54795463
""")
54805464

numpy/core/_internal.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,48 @@ def array_ufunc_errmsg_formatter(ufunc, method, *inputs, **kwargs):
670670
return ('operand type(s) do not implement __array_ufunc__'
671671
'({!r}, {!r}, {}): {}'
672672
.format(ufunc, method, args_string, types_string))
673+
674+
def _ufunc_doc_signature_formatter(ufunc):
675+
"""
676+
Builds a signature string which resembles PEP 457
677+
678+
This is used to construct the first line of the docstring
679+
"""
680+
681+
# input arguments are simple
682+
if ufunc.nin == 1:
683+
in_args = 'x'
684+
else:
685+
in_args = ', '.join('x{}'.format(i+1) for i in range(ufunc.nin))
686+
687+
# output arguments are both keyword or positional
688+
if ufunc.nout == 0:
689+
out_args = ', /, out=()'
690+
elif ufunc.nout == 1:
691+
out_args = ', /, out=None'
692+
else:
693+
out_args = '[, {positional}], / [, out={default}]'.format(
694+
positional=', '.join(
695+
'out{}'.format(i+1) for i in range(ufunc.nout)),
696+
default=repr((None,)*ufunc.nout)
697+
)
698+
699+
# keyword only args depend on whether this is a gufunc
700+
kwargs = (
701+
", casting='same_kind'"
702+
", order='K'"
703+
", dtype=None"
704+
", subok=True"
705+
"[, signature"
706+
", extobj]"
707+
)
708+
if ufunc.signature is None:
709+
kwargs = ", where=True" + kwargs
710+
711+
# join all the parts together
712+
return '{name}({in_args}{out_args}, *{kwargs})'.format(
713+
name=ufunc.__name__,
714+
in_args=in_args,
715+
out_args=out_args,
716+
kwargs=kwargs
717+
)

0 commit comments

Comments
 (0)
0