8000 DEP: Deprecate NumPy object scalars by seberg · Pull Request #16850 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DEP: Deprecate NumPy object scalars #16850

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 1 commit into from
Jul 21, 2020
Merged
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
DEP: Deprecate NumPy object scalars
NumPy object scalars should not exist, since they should always
just return the actual scalar in the first place. Put a deprecation
warning in place to be rapidly changed into a TypeError.

Once allocating it gives an error, we can make all methods raise
errors (with the exception of `__new__`.
  • Loading branch information
seberg committed Jul 13, 2020
commit 6e7a43b3f70b1ce9830b17629a066ad1d91736ad
26 changes: 26 additions & 0 deletions numpy/core/src/multiarray/scalartypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -2571,6 +2571,31 @@ void_dealloc(PyVoidScalarObject *v)
Py_TYPE(v)->tp_free(v);
}


static PyObject *
object_arrtype_alloc(PyTypeObject *type, Py_ssize_t items)
{
/*
* Object scalars should not actually exist, if they exist we should
* consider it to be a bug.
*/
static PyObject *visibleDeprecationWarning = NULL;
npy_cache_import("numpy", "VisibleDeprecationWarning",
&visibleDeprecationWarning);
if (visibleDeprecationWarning == NULL) {
return NULL;
}
if (PyErr_WarnEx(visibleDeprecationWarning,
"Creating a NumPy object scalar. NumPy object scalars should "
"never be created. If you see this message please inform the "
"NumPy developers. Since this message should never be shown "
Copy link
Member

Choose a reason for hiding this comment

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

Should we link to the issue tracker 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 think it is fine like this, people who might be doing this should know where to find us.

"this will raise a TypeError in the future.", 1) < 0) {
return NULL;
}
return gentype_alloc(type, items);
}


static void
object_arrtype_dealloc(PyObject *v)
{
Expand Down Expand Up @@ -3298,6 +3323,7 @@ NPY_NO_EXPORT PyTypeObject PyObjectArrType_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "numpy.object_",
.tp_basicsize = sizeof(PyObjectScalarObject),
.tp_alloc = object_arrtype_alloc,
.tp_dealloc = (destructor)object_arrtype_dealloc,
.tp_as_sequence = &object_arrtype_as_sequence,
.tp_as_mapping = &object_arrtype_as_mapping,
Expand Down
0