8000 ENH: Add a `__dict__` to ufunc objects and allow overriding `__doc__` by mtsokol · Pull Request #27735 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add a __dict__ to ufunc objects and allow overriding __doc__ #27735

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 7 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Deprecated _add_newdoc_ufunc
  • Loading branch information
mtsokol committed Nov 12, 2024
commit a507bf6792ca06fabf1609941fb7940d0eb58960
6 changes: 5 additions & 1 deletion doc/release/upcoming_changes/27735.new_feature.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* UFuncs now support ``__dict__`` attribute and allow overriding ``__doc__``
(either directly or via ``ufunc.__dict__["__doc__"]``). ``__dict__`` can be
used to also override other properties, such as ``__module__`` or ``__qualname__``.
used to also override other properties, such as ``__module__`` or
``__qualname__``.

* ``_add_newdoc_ufunc`` is now deprecated. ``ufunc.__doc__ = newdoc`` should
be used instead.
Copy link
Member

Choose a reason for hiding this comment

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

Nit, but it should be split into two files (different categories).

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah right, done!

4 changes: 2 additions & 2 deletions numpy/_core/src/umath/_struct_ufunc_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ PyMODINIT_FUNC PyInit__struct_ufunc_tests(void)
import_umath();

add_triplet = PyUFunc_FromFuncAndData(NULL, NULL, NULL, 0, 2, 1,
PyUFunc_None, "add_triplet",
"add_triplet_docstring", 0);
PyUFunc_None, "add_triplet",
NULL, 0);

dtype_dict = Py_BuildValue("[(s, s), (s, s), (s, s)]",
"f0", "u8", "f1", "u8", "f2", "u8");
Expand Down
7 changes: 7 additions & 0 deletions numpy/_core/src/umath/umathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) {
PyObject *
add_newdoc_ufunc(PyObject *NPY_UNUSED(dummy), PyObject *args)
{

/* 2024-11-12, NumPy 2.2 */
if (DEPRECATE("_add_newdoc_ufunc is deprecated. "
"Use `ufunc.__doc__ = newdoc` instead.") < 0) {
return NULL;
}

PyUFuncObject *ufunc;
PyObject *str;
if (!PyArg_ParseTuple(args, "O!O!:_add_newdoc_ufunc", &PyUFunc_Type, &ufunc,
Expand Down
11 changes: 11 additions & 0 deletions numpy/_core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)

from numpy._core._multiarray_tests import fromstring_null_term_c_api
import numpy._core._struct_ufunc_tests as struct_ufunc

try:
import pytz
Expand Down Expand Up @@ -732,3 +733,13 @@ def test_deprecated(self):
self.assert_deprecated(np.save, args=sample_args,
kwargs={'allow_pickle': allow_pickle,
'fix_imports': False})


class TestAddNewdocUFunc(_DeprecationTestCase):
# Deprecated in Numpy 2.2, 2024-11
def test_deprecated(self):
self.assert_deprecated(
lambda: np._core.umath._add_newdoc_ufunc(
struct_ufunc.add_triplet, "new docs"
)
)
2 changes: 2 additions & 0 deletions numpy/_core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -4884,9 +4884,11 @@ def func():


class TestAdd_newdoc_ufunc:
@pytest.mark.filterwarnings("ignore:_add_newdoc_ufunc:DeprecationWarning")
def test_ufunc_arg(self):
assert_raises(TypeError, ncu._add_newdoc_ufunc, 2, "blah")
assert_raises(ValueError, ncu._add_newdoc_ufunc, np.add, "blah")

@pytest.mark.filterwarnings("ignore:_add_newdoc_ufunc:DeprecationWarning")
def test_string_arg(self):
assert_raises(TypeError, ncu._add_newdoc_ufunc, np.add, 3)
Loading
0