diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2019-05-30 07:41:49 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-30 07:41:49 -0700 |
commit | 9c44a2dc63d5e63b7d36c57a8bea84c3d117130e (patch) | |
tree | 8502f2256f311fac53f61411827a3b5421194032 /doc/neps | |
parent | a8313fe411ef3a25c39a112bdf1f60a758ce9644 (diff) | |
parent | 5e8a4a11a900d9427368fe1f10c8086290ff01f1 (diff) | |
download | numpy-9c44a2dc63d5e63b7d36c57a8bea84c3d117130e.tar.gz |
Merge pull request #13633 from shoyer/nep-18-subclass-warning
DOC: caution against relying upon NumPy's implementation in subclasses
Diffstat (limited to 'doc/neps')
-rw-r--r-- | doc/neps/nep-0018-array-function-protocol.rst | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/doc/neps/nep-0018-array-function-protocol.rst b/doc/neps/nep-0018-array-function-protocol.rst index 01bd22522..27a462239 100644 --- a/doc/neps/nep-0018-array-function-protocol.rst +++ b/doc/neps/nep-0018-array-function-protocol.rst @@ -349,19 +349,27 @@ with ``__array_ufunc__``, so ``numpy.ndarray`` also defines a This method matches NumPy's dispatching rules, so for most part it is possible to pretend that ``ndarray.__array_function__`` does not exist. +The private ``_implementation`` attribute, defined below in the +``array_function_dispatch`` decorator, allows us to avoid the special cases for +NumPy arrays that were needed in the ``__array_ufunc__`` protocol. The ``__array_function__`` protocol always calls subclasses before superclasses, so if any ``ndarray`` subclasses are involved in an operation, they will get the chance to override it, just as if any other argument -overrides ``__array_function__``. However, the default behavior in an operation +overrides ``__array_function__``. But the default behavior in an operation that combines a base NumPy array and a subclass is different: if the subclass returns ``NotImplemented``, NumPy's implementation of the function will be called instead of raising an exception. This is appropriate since subclasses are `expected to be substitutable <https://en.wikipedia.org/wiki/Liskov_substitution_principle>`_. -Note that the private ``_implementation`` attribute, defined below in the -``array_function_dispatch`` decorator, allows us to avoid the special cases for -NumPy arrays that were needed in the ``__array_ufunc__`` protocol. +We still caution authors of subclasses to exercise caution when relying +upon details of NumPy's internal implementations. It is not always possible to +write a perfectly substitutable ndarray subclass, e.g., in cases involving the +creation of new arrays, not least because NumPy makes use of internal +optimizations specialized to base NumPy arrays, e.g., code written in C. Even +if NumPy's implementation happens to work today, it may not work in the future. +In these cases, your recourse is to re-implement top-level NumPy functions via +``__array_function__`` on your subclass. Changes within NumPy functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |