Skip to content

Numpy Datatype: Enable np.dtype() type conversions#1856

Open
franzpoeschel wants to merge 9 commits intoopenPMD:devfrom
franzpoeschel:dataset-constructor-conversions
Open

Numpy Datatype: Enable np.dtype() type conversions#1856
franzpoeschel wants to merge 9 commits intoopenPMD:devfrom
franzpoeschel:dataset-constructor-conversions

Conversation

@franzpoeschel
Copy link
Contributor

@franzpoeschel franzpoeschel commented Mar 2, 2026

This makes it possible to use things like np.int_ or "double" in the Dataset constructor, which currently has to be written as np.dtype(np.int_) and np.dtype("double"). Check also examples and tests.
Affects Datatype() constructor as well as RecordComponent.make_empty().

Future: Derive Dataset definition from Numpy array directly

@franzpoeschel franzpoeschel force-pushed the dataset-constructor-conversions branch from eb75a66 to 3d996b7 Compare March 2, 2026 17:29
ci rerun
@franzpoeschel franzpoeschel force-pushed the dataset-constructor-conversions branch from 3d996b7 to a4c6bf9 Compare March 2, 2026 18:23
@franzpoeschel franzpoeschel requested a review from ax3l March 3, 2026 10:21
Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>
Comment on lines +123 to +127
try
{
create_dtype = numpy.attr("dtype");
}
catch (std::exception const &e)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe check if it has the attribute and then throw? Avoids one catch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have removed those try-catch blocks which should only throw if the numpy interfaces change. This leaves those two blocks that check if numpy is installed and for any errors when calling numpy.dtype() with user-supplied objects.

@franzpoeschel franzpoeschel force-pushed the dataset-constructor-conversions branch from 752d517 to 6c346d0 Compare March 4, 2026 12:31
@franzpoeschel franzpoeschel requested a review from ax3l March 4, 2026 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: new additions to the API frontend: Python3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants