Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: Show full PEP 457 argument lists for ufuncs #9026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/reference/ufuncs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ them by defining certain special methods. For details, see
:class:`ufunc`
==============

.. _ufuncs.kwargs:

Optional keyword arguments
--------------------------

Expand Down
58 changes: 21 additions & 37 deletions numpy/add_newdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5421,40 +5421,19 @@ def luf(lamdaexpr, *args, **kwargs):
"""
Functions that operate element by element on whole arrays.

To see the documentation for a specific ufunc, use np.info(). For
example, np.info(np.sin). Because ufuncs are written in C
To see the documentation for a specific ufunc, use `info`. For
example, ``np.info(np.sin)``. Because ufuncs are written in C
(for speed) and linked into Python with NumPy's ufunc facility,
Python's help() function finds this page whenever help() is called
on a ufunc.

A detailed explanation of ufuncs can be found in the "ufuncs.rst"
file in the NumPy reference guide.
A detailed explanation of ufuncs can be found in the docs for :ref:`ufuncs`.

Unary ufuncs:
=============
Calling ufuncs:
===============

op(X, out=None)
Apply op to X elementwise

Parameters
----------
X : array_like
Input array.
out : array_like
An array to store the output. Must be the same shape as `X`.

Returns
-------
r : array_like
`r` will have the same shape as `X`; if out is provided, `r`
will be equal to out.

Binary ufuncs:
==============

op(X, Y, out=None)
Apply `op` to `X` and `Y` elementwise. May "broadcast" to make
the shapes of `X` and `Y` congruent.
op(*x[, out], where=True, **kwargs)
Apply `op` to the arguments `*x` elementwise, broadcasting the arguments.

The broadcasting rules are:

Expand All @@ -5463,18 +5442,23 @@ def luf(lamdaexpr, *args, **kwargs):

Parameters
----------
X : array_like
First input array.
Y : array_like
Second input array.
out : array_like
An array to store the output. Must be the same shape as the
output would have.
*x : array_like
Copy link
Member

@charris charris May 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each of the * need escaping like so \\* for sphinx. If you make it a raw string you can use a single \.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file never actually reaches sphinx anyway, but I was under the impression that this worked correctly due to the transformation that the Parameters section undergoes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had to fix other uses before as the docs didn't render correctly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this doesn't even get rendered, so no danger of it getting rendered incorrectly!

Since the only place this is ever seen is in the terminal, I'd argue it's better without backslashes.

I'd also argue that to keep the terminal tidy, the fix should go in the numpy sphinx extension, not here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that having numpydoc escape the *'s when generating the sphinx input would be nice, but it doesn't at this time. If this never showed up in the documentation, that would not be a problem, but I assume you would like it to eventually show up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but I assume you would like it to eventually show up.

Not really - AFAICT, the sole purposed of this page is to redirect the user to np.info or the ufunc docs. if ufunc.__doc__ is made visible in the web docs, then IMO, it should be the contents of ufuncs.rst.

This docstring aside - I was pretty sure that **kwargs was parsed correctly in the other docstring modified by this changeset - does the issue only exist for single asterisks?

Input arrays.
out : ndarray or tuple of ndarray, optional
Alternate array object(s) in which to put the result; if provided, it
must have a shape that the inputs broadcast to.
where : array_like, optional
Values of True indicate to calculate the ufunc at that position, values
of False indicate to leave the value in the output alone.
**kwargs
For other keyword-only arguments, see the :ref:`ufunc docs <ufuncs.kwargs>`.

Returns
-------
r : array_like
The return value; if out is provided, `r` will be equal to out.
r : ndarray or tuple of ndarray
`r` will have the shape that the arrays in `x` broadcast to; if `out` is
provided, `r` will be equal to `out`. If the function has more than one
output, then the result will be a tuple of arrays.

""")

Expand Down
45 changes: 45 additions & 0 deletions numpy/core/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Loading