Skip to content

Inconsistent behavior of dtype.descr with metadata #15488

Open
@dmbelov

Description

@dmbelov

We used to the following property: if x is a dtype then numpy.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 that descr returned by dtype can always be evaluated back to a dtype?

Related issues: h5py/h5py#1307

Reproducing code example:

import numpy as np

# 1. metadata is lost for simple dtype
s_dt = np.dtype('S8', metadata={'msg': 'Hello'})
print(s_dt.descr)
# prints [('', '|S8')]

# 2. metadata is returned for structured dtype, but it cannot be evaluated into dtype
struct_dtype = np.dtype([('a', s_dt)])
print(struct_dtype.descr)
# prints [('a', ('|S8', {'msg': 'Hello'}))]

# next line raises a ValueError exception
# ValueError: invalid shape in fixed-type tuple.
np.dtype(struct_dtype.descr)

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]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions