Skip to content
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

ENH: Add __array_ufunc__ #8247

Merged
merged 43 commits into from
Apr 27, 2017
Merged

ENH: Add __array_ufunc__ #8247

merged 43 commits into from
Apr 27, 2017

Commits on Apr 27, 2017

  1. ENH: Revert "Temporarily disable __numpy_ufunc__"

    This reverts commit bac094c and enables
    __numpy_ufunc__ for development in the NumPy 1.13.0 development cycle.
    charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    4fd7e84 View commit details
    Browse the repository at this point in the history
  2. ENH: Rename __numpy_ufunc__ to __array_ufunc__.

    The first commit in changing __numpy_ufunc__ by removing the index
    argument and making the out argument value always a tuple. These changes
    were proposed in numpygh-5986 and have been accepted. Renaming before further
    changes avoids triggering tests in scipy and astropy while keeping the
    numpy tests working.
    charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    fcd11d2 View commit details
    Browse the repository at this point in the history
  3. ENH: Remove position arg from __array_ufunc__.

    Previously when __array_ufunc__ for one of the ufunc arguments was
    called, that arguments position was passed in the call. This PR removes
    that argument as proposed in numpygh-5986.
    charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    c7b25e2 View commit details
    Browse the repository at this point in the history
  4. MAINT: Put PyArray_GetAttrString_SuppressException in get_attr_string.h

    This is an ugly kluge, but until we merge multiarray.so and umath.so
    moving stuff into private/*.h serves as a reasonable workaround.
    njsmith authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    8a9e790 View commit details
    Browse the repository at this point in the history
  5. MAINT: dike out a bunch of weird old code implementing scalar power

    For some reason, np.generic.__pow__ felt the need to reimplement
    Python's full binop dispatch logic. The code has been there since the
    beginning of (git) history, isn't required to pass any tests, and
    AFAICT doesn't actually do anything interesting. I suspect it was just
    split apart from the (much more trivial) implementation of all the
    other np.generic binop methods because nb_power has a slightly
    different signature than all the others (it takes an extra argument,
    which we ignore, but still has to be accepted and passed as
    appropriate), and then got forgotten when the other implementations
    were simplified long ago.
    
    This commit throws away all that code and makes gentype_power a
    2-liner, just like all the other gentype_@name@ methods.
    njsmith authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    4dd5380 View commit details
    Browse the repository at this point in the history
  6. BUG/ENH: Switch to simplified __array_ufunc__/binop interaction

    As per the discussion at numpygh-5844, and in particular
       numpy#5844 (comment)
    this commit switches binop dispatch to mostly defer to ufuncs, except
    in some specific cases elaborated in a long comment in number.c.
    
    The basic strategy is to define a single piece of C code that knows
    how to handle forward binop overrides, and we put it into
    private/binop_override.h so that it can be accessed by both the array
    code in multiarray.so and the scalar code in umath.so.
    njsmith authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    7d9bc2f View commit details
    Browse the repository at this point in the history
  7. MAINT: allow __array_ufunc__ = None to force binops to defer.

    In previous versions, one could force ndarray binops to defer by
    setting a high __array_priority__.  With __array_ufunc__ this gets
    ignored, and this commit ensures it is still possible to avoid
    using the standard python language feature that setting something
    to None means it is not implemented. In consequence, inside a
    ufunc, if __array_ufunc__ is None, it will be treated as if it
    had returned NotImplemented (leading to a TypeError if no other
    object had a functioning __array_ufunc__ override).
    mhvk authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    e4b5163 View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    2e6d8c0 View commit details
    Browse the repository at this point in the history
  9. Configuration menu
    Copy the full SHA
    d5c5ac1 View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    3124e96 View commit details
    Browse the repository at this point in the history
  11. Configuration menu
    Copy the full SHA
    6a3ca31 View commit details
    Browse the repository at this point in the history
  12. Configuration menu
    Copy the full SHA
    79bb733 View commit details
    Browse the repository at this point in the history
  13. Configuration menu
    Copy the full SHA
    7c3dc5a View commit details
    Browse the repository at this point in the history
  14. DOC: Describe __array_func__ in subclassing

    This includes the use of super everywhere, and in the brief
    description of __array_ufunc__ in the reference section.
    mhvk authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    71201d2 View commit details
    Browse the repository at this point in the history
  15. ENH: implement ndarray.__array_ufunc__

    mhvk authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    3041710 View commit details
    Browse the repository at this point in the history
  16. Configuration menu
    Copy the full SHA
    5fe6fc6 View commit details
    Browse the repository at this point in the history
  17. MAINT: let ndarray.__array_ufunc__ bail if any overrides are in place.

    As part of this, split off the checking for overrides from the actual
    trying to execute them (in ufunc_override.c), so that just the check
    can be done in ndarray.__array_ufunc__.
    mhvk authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    e092823 View commit details
    Browse the repository at this point in the history
  18. MAINT: Update array_ufunc NEP.

    Bring into compliance with current ndarray.__array_ufunc__
    implementation and type casting hierarchy.
    pv authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    1147894 View commit details
    Browse the repository at this point in the history
  19. Configuration menu
    Copy the full SHA
    e325a10 View commit details
    Browse the repository at this point in the history
  20. Configuration menu
    Copy the full SHA
    39c2273 View commit details
    Browse the repository at this point in the history
  21. DOC: clarify use of super and getattr

    mhvk authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    6b41d11 View commit details
    Browse the repository at this point in the history
  22. DOC: update NEP again.

    mhvk authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    0ede0e9 View commit details
    Browse the repository at this point in the history
  23. Configuration menu
    Copy the full SHA
    5f9252c View commit details
    Browse the repository at this point in the history
  24. BUG,MAINT: ensure out=None is never passed on to __array_ufunc__.

    As part of this, it turns out the number of output arguments nout
    was needed, so that was passed in. Also, testing showed that arguments
    passed in both as positional and keyword arguments were not caught,
    so a duplication check was added.
    mhvk authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    8cc2f71 View commit details
    Browse the repository at this point in the history
  25. DOC: remove left-over piece discussing binops

    The text had been changed following the suggestion of @njsmith, but
    somehow I had forgotten to delete the original.
    mhvk authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    856da73 View commit details
    Browse the repository at this point in the history
  26. Configuration menu
    Copy the full SHA
    2b6c7fd View commit details
    Browse the repository at this point in the history
  27. Configuration menu
    Copy the full SHA
    36e8494 View commit details
    Browse the repository at this point in the history
  28. MAINT: simplify now that __array_ufunc__ overrides ufuncs only.

    In particular, use fact that we're guaranteed to have a PyUFuncObject
    in PyUFunc_CheckOverride.
    mhvk authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    55500b9 View commit details
    Browse the repository at this point in the history
  29. MAINT: split out umath-specific part of ufunc_override.

    And put it in src/umath instead of src/private. This leaves only
    the PyUFunc_WithOverride function in ufunc_override.c, which is all
    that is needed outside of umath (it is called by array_ufunc in
    multiarray/methods.c).
    mhvk authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    25e973d View commit details
    Browse the repository at this point in the history
  30. Configuration menu
    Copy the full SHA
    b1fa10a View commit details
    Browse the repository at this point in the history
  31. Configuration menu
    Copy the full SHA
    1de8f5a View commit details
    Browse the repository at this point in the history
  32. Configuration menu
    Copy the full SHA
    a460015 View commit details
    Browse the repository at this point in the history
  33. Configuration menu
    Copy the full SHA
    cd2e42c View commit details
    Browse the repository at this point in the history
  34. BUG: Add back removed elision code.

    Code inadvertantly removed in rebasing old PR on new work.
    charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    ff628f1 View commit details
    Browse the repository at this point in the history
  35. DOC,TST: clarify example of ndarray subclass using __array_ufunc__

    Also add a few more tests of the same example for good measure.
    mhvk authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    1fc6e63 View commit details
    Browse the repository at this point in the history
  36. BUG: Support nout == 0 and at method

    eric-wieser authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    a431743 View commit details
    Browse the repository at this point in the history
  37. Configuration menu
    Copy the full SHA
    1e460b7 View commit details
    Browse the repository at this point in the history
  38. ENH: Add NDArrayOperatorsMixin mixin class.

    This mixin class provides an easy way to implement arithmetic operators
    that defer to __array_ufunc__ like numpy.ndarray in non-ndarray
    subclasses.
    shoyer authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    02600d3 View commit details
    Browse the repository at this point in the history
  39. Configuration menu
    Copy the full SHA
    d3ff023 View commit details
    Browse the repository at this point in the history
  40. Configuration menu
    Copy the full SHA
    b9359f1 View commit details
    Browse the repository at this point in the history
  41. Configuration menu
    Copy the full SHA
    256a8ae View commit details
    Browse the repository at this point in the history
  42. ENH: Better error message for __array_ufunc__ not implemented

    * ENH: Better error message for __array_ufunc__ not implemented
    
    New behavior:
    
        >>> import numpy as np
    
        >>> class Dummy:
        ...     def __array_ufunc__(self, *args, **kwargs):
        ...         return NotImplemented
    
        >>> np.negative(Dummy())
        TypeError: operand type(s) do not implement __array_ufunc__(
        <ufunc 'negative'>, '__call__', <__main__.Dummy object at 0x1106df8d0>):
        'Dummy'
    
    * check for null errmsg_formatter
    shoyer authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    3272a86 View commit details
    Browse the repository at this point in the history
  43. ENH: NDArrayOperatorsMixin calls ufuncs directly, like ndarray

    * ENH: NDArrayOperatorsMixin calls ufuncs directly, like ndarray
    
    Per our discussion in
    numpy#8247 (comment)
    
    * add back in accidentally dropped __repr__
    shoyer authored and charris committed Apr 27, 2017
    Configuration menu
    Copy the full SHA
    32221df View commit details
    Browse the repository at this point in the history