Skip to content

BUG: reject structured DTypes that contain StringDType fields - #32027

Merged
seberg merged 3 commits into
numpy:mainfrom
ngoldbaum:reject-stringdtype-structured
Jul 19, 2026
Merged

seberg merged 3 commits into
numpy:mainfrom
ngoldbaum:reject-stringdtype-structured

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Jul 17, 2026 •

Copy link
Copy Markdown
Member

PR summary

Currently we reject structured dtypes that contain StringDType in only one code path:

if (PyObject_IsInstance((PyObject *)conv, (PyObject *)&PyArray_StringDType)) {
PyErr_Format(PyExc_TypeError,
"StringDType is not currently supported for structured dtype fields.");
goto fail;
}

This rejects dtypes like np.dtype([("a", StringDType())]) but lets through dtypes like [("a", (T, 10))] or "T,i4".

This PR closes those holes.

There's also a reference leak in the existing error path, which this fixes.

Open questions

Unfortunately people may be depending on the old, broken support. I'm not sure if closing these holes should trigger deprecation warnings?

Alternatively we could try to actually support StringDType subarrays, #32012 (comment) might have a path towards that.

AI Disclosure

I used an AI model to identify the code paths that need new errors. It also spotted the reference leak.

Comment thread numpy/_core/src/multiarray/descriptor.c Outdated
descr = PyDataType_SUBARRAY(descr)->base;
}
if (PyObject_IsInstance((PyObject *)descr,
(PyObject *)&PyArray_StringDType)) {

@seberg seberg Jul 17, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the quick fix! Rather than this, can you check for it defining the "finalizer" slot? That is (in principle) more generic, even if string dtype is the only one right now.
(But maybe this is important for BytesDType :) for example).

Unfortunately people may be depending on the old, broken support. I'm not sure if closing these holes should trigger deprecation warnings?

Hmmmpf, yeah... I would prefer to just disable it and hope for the best, but can go either way.

Another thing that would make this simpler/nicer is if we also reject subarray dtype creation like this. Then we don't have to iterate bases at all here.
(Honestly, maybe you already do here, but then we don't need to iterate.)

(It's not like subarrays work, they only "work", because you can't actually attach them to an array in practice...)

@seberg seberg added the 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. label Jul 17, 2026
@mhvk

mhvk commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Not sure I have much to add, though in the end it would be nice to just support StringDType in structured arrays, just like we support object.

@seberg seberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, for fixing this and converting to check for finalize_descr! looking I wonder if we could put this check to later to avoid duplicating, but with a helper it doesn't matter much.

Good to get this fixed!

@seberg
seberg merged commit 941bc42 into numpy:main Jul 19, 2026
89 checks passed
ngoldbaum added a commit to ngoldbaum/numpy that referenced this pull request Aug 4, 2026
ngoldbaum added a commit to ngoldbaum/numpy that referenced this pull request Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants