summaryrefslogtreecommitdiff
path: root/numpy/core/overrides.py
Commit message (Collapse)AuthorAgeFilesLines
* MAINT: remove `NUMPY_EXPERIMENTAL_ARRAY_FUNCTION` env varRalf Gommers2023-03-121-13/+1
| | | | | | | | As discussed in https://mail.python.org/archives/list/numpy-discussion@python.org/thread/UKZJACAP5FUG7KP2AQDPE4P5ADNWLOHZ/ This flag was always meant to be temporary, and cleaning it up is long overdue.
* DOC: Adept internal docs a bit based on reviewSebastian Berg2023-01-171-2/+4
|
* ENH: Improve array function overhead by using vectorcallSebastian Berg2023-01-171-57/+37
| | | | | | | | | | | | | | | | | | | | | | | | This moves dispatching for `__array_function__` into a C-wrapper. This helps speed for multiple reasons: * Avoids one additional dispatching function call to C * Avoids the use of `*args, **kwargs` which is slower. * For simple NumPy calls we can stay in the faster "vectorcall" world This speeds up things generally a little, but can speed things up a lot when keyword arguments are used on lightweight functions, for example:: np.can_cast(arr, dtype, casting="same_kind") is more than twice as fast with this. There is one alternative in principle to get best speed: We could inline the "relevant argument"/dispatcher extraction. That changes behavior in an acceptable but larger way (passes default arguments). Unless the C-entry point seems unwanted, this should be a decent step in the right direction even if we want to do that eventually, though. Closes gh-20790 Closes gh-18547 (although not quite sure why)
* Merge pull request #22619 from seberg/move-set_moduleMatti Picus2022-11-291-19/+2
|\ | | | | MAINT: Move set_module from numpy.core to numpy._utils
| * MAINT: Move _inspect and _pep440 from compat to _utilsSebastian Berg2022-11-251-1/+1
| | | | | | | | | | Note that unfortunately, compat does expose _inspect as well, so the import remains (just the definition place moves).
| * MAINT: Move set_module to numpy.core to use without C importSebastian Berg2022-11-241-18/+1
| |
* | API: Add numpy.testing.overrides to aid testing of custom array containersNathan Goldbaum2022-11-161-0/+4
|/ | | | Closes #15544
* Removed two unused importsCallum O'Riley2022-07-141-1/+0
|
* ENH: Ensure dispatcher TypeErrors report original nameSebastian Berg2022-06-101-1/+22
| | | | | | | | | | | | | | | This replaces the name in the TypeError with the actually raised name. In principle we could add one more check, because a signature related TypeError must have a traceback with exactly one entry (so `sys.exc_info()[2].tb_next is None`). In practice this seems unnecessary though. This ensures the following message: >>> np.histogram(asdf=3) TypeError: histogram() got an unexpected keyword argument 'asdf' Closes gh-21647
* DOC: add note about array_like being optionalmelissawm2022-02-081-1/+1
|
* MAINT: LGTM.com recommendation: Unused importDimitri Papadopoulos2021-10-071-1/+0
| | | | | | Import of 'histogram' is not used. Import of 'histogramdd' is not used. Import of 'textwrap' is not used.
* Update numpy/core/overrides.pySebastian Berg2021-08-251-2/+2
| | | | Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
* remove import time compileIrit Katriel2021-08-251-30/+8
|
* NEP: Accept NEP 35 as finalSebastian Berg2021-06-071-5/+1
| | | | | | | | | | This accepts NEP 35 as final. There has been no discussion about it in a long time. The current mode is strict about type input (`like=` must be an array-like). So that most of the "open" points are OK to remain open. Unless we need to discuss the name `like` or the fact that we pass an array-like itself, the previously noted open points gh-17075 all seem not very relevant anymore.
* ENH: implement NEP-35's `like=` argument (gh-16935)Peter Andreas Entschev2020-08-281-0/+21
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This PR adds the implementation of NEP-35's like= argument, allowing dispatch of array creation functions with __array_function__ based on a reference array. * ENH: Add like= kwarg via __array_function__ dispatcher to asarray * ENH: Add new function for __array_function__ dispatching from C This new function allows dispatching from C directly, while also implementing the new `like=` argument, requiring only minimal changes to existing array creation functions that need to add support for that argument. * ENH: Add like= support to numpy.array The implementation uses array_implement_c_array_function, thus introducing minimal complexity to the original _array_fromobject code. * BUG: Fix like= dispatcher for np.full * ENH: Remove np.asarray like= dispatcher via Python np.asarray can rely on np.array's C dispatcher instead. * TST: Add some tests for like= argument Tests comprise some of the functions that have been implemented already: * np.array (C dispatcher) * np.asarray (indirect C dispatcher via np.array) * np.full (Python dispatcher) * np.ones (Python dispatcher) * ENH: Remove like= argument during array_implement_array_function * ENH: Add like= kwarg to ones and full * BUG: prevent duplicate removal of `like=` argument * ENH: Make `like=` a keyword-only argument * ENH: Use PyUnicode_InternFromString in arrayfunction_override Replace PyUnicode_FromString by PyUnicode_InternFromString to cache "like" string. * ENH: Check for arrayfunction_override import errors Check and handle errors on importing NumPy's Python functions * BUG: Fix array_implement_c_array_function error handling * ENH: Handle exceptions with C implementation of `like=` * ENH: Add `like=` dispatch for all asarray functions Using Python dispatcher for all of them. Using the C dispatcher directly on the `np.array` call can result in incorrect behavior. Incorrect behavior may happen if the downstream library's implementation is different or if not all keyword arguments are supported. * ENH: Simplify handling of exceptions with `like=` * TST: Add test for exception handling with `like=` * ENH: Add support for `like=` to `np.empty` and `np.zeros` * TST: Add `like=` tests for `np.empty` and `np.zeros` * ENH: Add `like=` to remaining multiarraymodule.c functions Functions are: * np.arange * np.frombuffer * np.fromfile * np.fromiter * np.fromstring * TST: Add tests for multiarraymodule.c functions with like= Functions are: * np.arange * np.frombuffer * np.fromfile * np.fromiter * np.fromstring * ENH: Add `like=` support to more creation functions Support for the following functions is added: * np.eye * np.fromfunction * np.genfromtxt * np.identity * np.loadtxt * np.tri * TST: Add `like=` tests for multiple functions Tests for the following functions are added: * np.eye * np.fromfunction * np.genfromtxt * np.identity * np.loadtxt * np.tri * TST: Reduce code duplication in `like=` tests * DOC: Document `like=` in functions that support it Add documentations for the following functions: * np.array * np.arange * np.asarray * np.asanyarray * np.ascontiguousarray * np.asfortranarray * np.require * np.empty * np.full * np.ones * np.zeros * np.identity * np.eye * np.tri * np.frombuffer * np.fromfile * np.fromiter * np.fromstring * np.loadtxt * np.genfromtxt * ENH: Add `like=` to numpy/__init__.pyi stubs * BUG: Remove duplicate `like=` dispatching in as*array Functions `np.asanyarray`, `np.contiguousarray` and `np.fortranarray` were dispatching both via their definitions and `np.array` calls, the latter should be avoided. * BUG: Fix missing check in array_implement_array_function * BUG: Add missing keyword-only markers in stubs * BUG: Fix duplicate keyword-only marker in array stub * BUG: Fix syntax error in numpy/__init__.pyi * BUG: Fix more syntax errors in numpy/__init__.pyi * ENH: Intern arrayfunction_override strings in multiarraymodule * STY: Add missing brackets to arrayfunction_override.c * MAINT: Remove arrayfunction_override dict check for kwarg * TST: Assert that 'like' is not in TestArrayLike kwargs * MAINT: Rename array_implement_c_array_function(_creation) This is done to be more explicit as to its usage being intended for array creation functions only. * MAINT: Use NotImplemented to indicate fallback to default * TST: Test that results with `like=np.array` are correct * TST: Avoid duplicating MyArray code in TestArrayLike * TST: Don't delete temp file, it may cause issues with Windows * TST: Don't rely on eval in TestArrayLike * TST: Use lambda with StringIO in TestArrayLike * ENH: Avoid unnecessary Py_XDECREF in arrayfunction_override * TST: Make TestArrayLike more readable * ENH: Cleaner error handling in arrayfunction_override * ENH: Simplify array_implement_c_array_function_creation * STY: Add missing spaces to multiarraymodule.c * STY: C99 declaration style in arrayfunction_override.c * ENH: Simplify arrayfunction_override.c further Remove cleanup label from array_implementation_c_array_function, simplifying the code. Fix unitialized variable warning in array_implementation_array_function_internal. * DOC: Use string replacement for `like=` documentation Avoid repeating the full text for the `like=` argument by storing it as a variable and using `replace` on each docstring. * DOC: Update `like=` docstring * TST: Test like= with array not implementing __array_function__ * TST: Add missing asanyarray to TestArrayLike * ENH: Use helper function for like= dispatching Avoid dispatching like= from Python implementation functions to improve their performance. This is achieved by only calling a dispatcher function when like is passed by the users. * ENH: Rename array_function_dispatch kwarg to public_api * BUG: Add accidentally removed decorator for np.eye back * DOC: Add set_array_function_like_doc function The function keeps Python files cleaner and resolve errors when __doc__ is not defined due to PYTHONOPTIMIZE or -OO . * DOC: Add mention to like= kwarg being experimental * TST: Test like= with not implemented downstream function * DOC: Fix like= docstring reference to NEP 35. * ENH: Prevent silent errors if public_api is not callable * ENH: Make set_array_function_like_doc a decorator * ENH: Simplify `_*_with_like` functions * BUG: Fix multiple `like=` dispatching in `require` * MAINT: Remove now unused public_api from array_function_dispatch Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
* DOC: Fix RST/numpydoc standard.Matthias Bussonnier2020-07-101-2/+2
| | | | | | | | One of the header line was not long enough, make it the same length as the title. The section "Arguments" is usually called "Parameters". Update for consistency.
* MAINT: import time: avoid repeated textwrap function dispatch instantiationMark Harfouche2019-07-251-7/+13
| | | | Avoid the repeated call of `dedent` when a single call is enough. This micro-optimizes the import time from around 100ms (or slightly above) by 4-6 ms. (PR: gh-14095)
* MAINT: Fixes tests with __array_function__ disabledStephan Hoyer2019-05-261-2/+2
|
* Tests for NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=0Stephan Hoyer2019-05-251-9/+2
|
* MAINT: revert __skip_array_function__ from NEP-18Stephan Hoyer2019-05-251-1/+22
| | | | | | xref GH-13624, GH-12028 TODO: update tests/CI for NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=0
* Merge branch 'master' into implement-numpy-implementationStephan Hoyer2019-05-131-2/+22
|\
| * ENH: invent a better fake filename than '<string'>Stephan Hoyer2019-05-111-1/+3
| |
| * ENH: use exec() instead array_function_dispatch to improve tracebacksStephan Hoyer2019-05-111-2/+20
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | xref GH-12028 Current behavior: >>> np.dot(None, None) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/shoyer/dev/numpy/numpy/core/overrides.py", line 175, in public_api implementation, public_api, relevant_args, args, kwargs) TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType' >>> np.stack([], invalid=True) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/shoyer/dev/numpy/numpy/core/overrides.py", line 148, in public_api relevant_args = dispatcher(*args, **kwargs) TypeError: _stack_dispatcher() got an unexpected keyword argument 'invalid' With this change: >>> np.dot(None, None) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 6, in dot TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType' >>> np.stack([], invalid=True) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 4, in stack TypeError: _stack_dispatcher() got an unexpected keyword argument 'invalid'
* | Rename to __skip_array_function__Stephan Hoyer2019-05-101-1/+1
| |
* | ENH: implement __numpy_implementation__ attribute for NEP-18Stephan Hoyer2019-04-221-0/+2
|/
* DEV: cleanup imports and some assignments (from LGTM)mattip2019-03-171-1/+0
|
* ENH: __array_function__ updates for NumPy 1.17.0Stephan Hoyer2019-01-221-18/+0
| | | | | | | | | | - Always enable __array_function__ overrides. - Remove special cases for Python 2 compatibility. - Document these changes in 1.17.0-notes.rst. It will be good to see ASV numbers to understand the performance implications of these changes. If need be, we can speed up NumPy functions internally by using non-dispatched functions (with ``.__wrapped__``).
* ENH: port __array_function__ overrides to CStephan Hoyer2018-12-191-94/+10
|
* ENH: refactor __array_function__ pure Python implementationStephan Hoyer2018-12-191-39/+69
|
* Merge pull request #12328 from mhvk/ndarray_array_function_allow_subclassesStephan Hoyer2018-12-021-11/+5
|\ | | | | MAINT: Allow subclasses in `ndarray.__array_function__`.
| * MAINT: Allow subclasses in ndarray.__array_function__.Marten van Kerkwijk2018-12-021-11/+5
| | | | | | | | The Liskov substitution principle suggests it should.
* | DOC: add docs_from_dispatcher to docstringStephan Hoyer2018-12-011-2/+6
| |
* | MAINT: remove wrapper functions from numpy.core.multiarrayStephan Hoyer2018-12-011-3/+24
|/ | | | | | | | | | | | | | | | | | | | The original motivation for the style of these wrapper functions, introduced in gh-12175, was to preserve introspection. But it turns out NumPy's functions defined in C don't support introspection anyways, so the extra wrapper functions are entirely pointless. This version reverts the additional wrapper functions, which put default arguments in two places and introduced slow-down due to the overhead of another function call. I've retained docstrings in multiarray.py, since it's definitely more readable to keep docstrings and dispatchers together rather than leaving docstrings in _add_newdocs.py. One bonus of this approach is that dispatcher functions have the same name as their implementations, so `np.concatenate(unknown=True)` gives an error message mentioning "concatenate" rather than "_concatenate_dispatcher": `TypeError: concatenate() got an unexpected keyword argument 'unknown'`
* ENH: set correct __module__ for objects in numpy's public APIStephan Hoyer2018-11-131-3/+3
| | | | | | | | | | | | | Fixes GH-12271 Tests verify that everything in ``dir(numpy)`` either has ``__module__`` set to ``'numpy'``, or appears in an explicit whitelist of undocumented functions and exported bulitins. These should eventually be documented or removed. I also identified a handful of functions for which I had accidentally not setup dispatch for with ``__array_function__`` before, because they were listed under "ndarray methods" in ``_add_newdocs.py``. I guess that should be a lesson in trusting code comments :).
* DOC: add docstring for override_moduleStephan Hoyer2018-11-111-1/+10
|
* DOC: add a full docstring for array_function_dispatchStephan Hoyer2018-11-111-1/+26
|
* MAINT: more fixes for disabling overridesStephan Hoyer2018-11-101-12/+22
|
* MAINT: fix disabling __array_function__Stephan Hoyer2018-11-101-1/+1
|
* MAINT: disable __array_function__ dispatch unless environment variable setStephan Hoyer2018-11-101-11/+16
| | | | | | | | | | | | Per discussion on the mailing list, __array_function__ isn't quite ready to release as part of NumPy 1.16: https://mail.python.org/pipermail/numpy-discussion/2018-November/078949.html We'd like to improve performance a bit, and it will be easier to support introspection on NumPy functions if we support Python 3 only. So for now, you need to set the environment variable ``NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=1`` to enable dispatching.
* MAINT: Simple speed-ups for getting overloaded typesMarten van Kerkwijk2018-11-031-11/+21
| | | | | Mostly aim to ensure that the most common case, in which the only override class is ndarray, is fast.
* Merge branch 'master' into fix-overloaded-reprStephan Hoyer2018-10-261-6/+10
|\
| * MAINT: improved error message for no __array_function__ implementationsStephan Hoyer2018-10-231-3/+8
| |
| * MAINT: Fix typo in commentwtli@Dirac2018-10-201-2/+2
| |
| * WIP: __array_ufunc__ for multiarray functionsStephan Hoyer2018-10-141-1/+1
| |
* | MAINT: add __wrapped__ for Python 2 supportStephan Hoyer2018-10-181-0/+5
|/
* ENH: Validate dispatcher functions in array_function_dispatch (#12099)Stephan Hoyer2018-10-081-1/+36
| | | | | | | | | | | | | * Validate dispatcher functions in array_function_dispatch They should have the same signature as the decorated function. Note: eventually these checks should be optional -- we really only need them to be run as part of NumPy's test suite, not every time numpy is imported. * ENH: make signature checking in array_function_dispatch optional * Change verify_signature keyword argument to verify
* CLN: rename array_function internal argumentsStephan Hoyer2018-09-281-38/+28
|
* CLN: cleanup and better document core/overrides.pyStephan Hoyer2018-09-261-26/+69
| | | | | I think this organization makes a little more sense, especially when thinking about what the functions we want to write in C should look like.
* Fix precedence of ndarray subclasses and misc cleanupStephan Hoyer2018-09-241-29/+37
|
* Add comment on subclasses orderingStephan Hoyer2018-09-231-0/+3
|