-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Inconsistent behavior of dtype.descr with metadata #15488
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
Comments
I don't think this property ever held, and my recommendation would be to avoid using |
We need a way to store dtype in text format. We are simply calling Regardless of the usage, it is unclear why |
Try this one ;)
These dtypes can appear more often than they used to too. |
The most correct option would be something like |
Possible duplicate of #14142 |
|
The fact is, that metadata breaks things here. But Do you have a proposal? I am not sure what best to do. We could try and drop metadata from |
Proposal
|
Unfortunately by design it doesn't really do this: >>> a = np.dtype('i4')
>>> a
dtype('int32')
>>> np.dtype(a.descr)
dtype([('f0', '<i4')]) So at best, we can achieve "vaguely close to the original" |
I am not sure what |
Maybe deprecate/make it private as part of the dtype refactor? |
Have you made any decision on what to do in this ticket? I just want to notify you that changing behavior of descr requires me, in particular, and may be many other people, to change code that had been working without any problems for years! Please be responsible, do not break other's people code. |
For now, I'd recommend you use something like gh-14994 in your own code, and avoid using |
So, basically you are forcing the whole NumPy community to change their code because YOU added a new feature that breaks everybody's code! This is not a good direction of development. Why cannot you just patch numpy by either
Both of these approaches make less damage to existing code than the one that your have chosen now. |
Which feature are you describing? I wasnt aware we were talking about a change that had already happened. |
Indeed, I do am not aware of any change either. The only change I am aware of is that h5py (and maybe others) suddenly started to actually use metadata. So the issue you have is reverse, we are hesitant to change behaviour here as to not force anyone to change code, although it is and probably was not super consistent. |
Thanks for your replies. I looked at the source code: the code for creation of dtype from list is written consistently, there is just a simple logical bug there. Look at the file // the BUG is on the next line:
// val might be dict, but metadata is NULL; in this case val is simply ignored.
//else if (type->metadata && (PyDict_Check(val) || PyDictProxy_Check(val))) {
// replace the line above by the following patch
// Start of PATCH
else if (PyDict_Check(val) || PyDictProxy_Check(val)) {
// if metadata is NULL, just create a new dictionary
if (!type->metadata) {
type->metadata = PyDict_New();
if (type->metadata == NULL) {
Py_DECREF(type);
return NULL;
}
}
// End of PATCH
if (PyDict_Merge(type->metadata, val, 0) == -1) {
Py_DECREF(type);
return NULL;
}
return type;
} I complied NumPy, and I checked that this code works on the following examples: dt = numpy.dtype([('a', ('S8', {'x': "Hello"}), 2)])
dt.descr
# [('a', ('|S8', {'x': 'Hello'}), (2,))]
numpy.dtype(dt.descr) # works
numpy.dtype(dt.descr) == dt
# returns True Could you please review this patch and add it to the closest NumPy update? |
eric-wieser, seberg are you going to include my patch shown in the previous update to the nearest NumPy release? |
@dmbelov, not sure, but could you open a PR for discussion rather than a patch in an issue? |
Would you mind sharing some actual code that actually uses |
We used to the following property: if
x
is a dtype thennumpy.dtype(x.descr)
is the same dtype. This property is broken in NumPy 1.8 when one specifies metadata to dtype. Is it possible to at least fix thatdescr
returned by dtype can always be evaluated back to a dtype?Related issues: h5py/h5py#1307
Reproducing code example:
Numpy/Python version information:
1.18.1 3.8.1 | packaged by conda-forge | (default, Jan 29 2020, 14:55:04)
[GCC 7.3.0]
The text was updated successfully, but these errors were encountered: