Skip to content

Commit 4eafc20

Browse files
authored
unpin numpy to enable working with numpy version 2 as well (#689)
1 parent 2cd16cd commit 4eafc20

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ dependencies = [
2727
"h5py>=3.6.0",
2828
"xarray>=0.20.2",
2929
"PyYAML>=6.0",
30-
'numpy>=1.22.4,<2.0.0',
30+
'numpy',
3131
"pandas>=1.3.2",
3232
"ase>=3.19.0",
3333
"mergedeep",
@@ -140,7 +140,7 @@ select = [
140140
"UP", # pyupgrade
141141
# "F401", # remove unused import
142142
"I001", # sort imports
143-
# "NPY201", # reactivate when np>2.0 is used
143+
"NPY201", # upgrade to numpy>2
144144
]
145145
ignore = [
146146
"E402", # Module level import not at top of file

src/pynxtools/dataconverter/hdfdict.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import h5py
99
import yaml
10-
from numpy import string_
1110

1211
TYPE = "_type_"
1312

@@ -45,7 +44,8 @@ def unpack_dataset(item):
4544
4645
"""
4746
value = item[()]
48-
type_id = item.attrs.get(TYPE, string_()).astype(str)
47+
type_id = item.attrs.get(TYPE, "")
48+
4949
if type_id == "datetime":
5050
if hasattr(value, "__iter__"):
5151
value = [datetime.fromtimestamp(ts) for ts in value]
@@ -62,7 +62,7 @@ def unpack_dataset(item):
6262
value = tuple(value)
6363

6464
elif type_id == "str":
65-
value = string_(value).astype(str)
65+
value = value.decode("utf-8") if isinstance(value, bytes) else value
6666

6767
return value
6868

@@ -181,15 +181,16 @@ def pack_dataset(hdfobject, key, value):
181181
attr_data = None
182182

183183
if attr_data:
184-
ds.attrs.create(name=TYPE, data=string_(attr_data))
184+
ds.attrs.create(name=TYPE, data=attr_data.encode("utf-8"))
185185

186186
except (TypeError, ValueError):
187187
# Obviously the data was not serializable. To give it
188188
# a last try; serialize it to yaml
189189
# and save it to the hdf file:
190-
ds = hdfobject.create_dataset(name=key, data=string_(yaml.safe_dump(value)))
191-
192-
ds.attrs.create(name=TYPE, data=string_("yaml"))
190+
ds = hdfobject.create_dataset(
191+
name=key, data=yaml.safe_dump(value).encode("utf-8")
192+
)
193+
ds.attrs.create(name=TYPE, data=b"yaml")
193194
# if this fails again, restructure your data!
194195

195196

tests/dataconverter/test_validation.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232

3333
from .test_helpers import alter_dict # pylint: disable=unused-import
3434

35+
# Workaround for different str representation of np.bool
36+
if np.lib.NumpyVersion(np.__version__) >= "2.0.0":
37+
np_bool = "numpy.bool"
38+
else:
39+
np_bool = "numpy.bool_"
40+
3541

3642
def set_to_none_in_dict(data_dict: Optional[Template], key: str, optionality: str):
3743
"""Helper function to forcefully set path to 'None'"""
@@ -468,8 +474,7 @@ def format_error_message(msg: str) -> str:
468474
"NOT_TRUE_OR_FALSE",
469475
),
470476
[
471-
"The value at /ENTRY[my_entry]/NXODD_name[nxodd_name]/bool_value should be one of the following Python types: "
472-
"(<class 'bool'>, <class 'numpy.bool_'>), as defined in the NXDL as NX_BOOLEAN."
477+
f"The value at /ENTRY[my_entry]/NXODD_name[nxodd_name]/bool_value should be one of the following Python types: (<class 'bool'>, <class '{np_bool}'>), as defined in the NXDL as NX_BOOLEAN."
473478
],
474479
id="string-instead-of-bool",
475480
),
@@ -1906,7 +1911,7 @@ def format_error_message(msg: str) -> str:
19061911
alter_dict(
19071912
TEMPLATE,
19081913
"/ENTRY[my_entry]/NXODD_name[nxodd_name]/int_value",
1909-
{"compress": np.int64(2), "strength": 11},
1914+
{"compress": 2, "strength": 11},
19101915
),
19111916
[
19121917
"Compression strength for /ENTRY[my_entry]/NXODD_name[nxodd_name]/int_value = "
@@ -2015,7 +2020,7 @@ def test_validate_data_dict(data_dict, error_messages, caplog, request):
20152020
),
20162021
[
20172022
"The value at /my_entry/nxodd_name/bool_value should be one of the following Python types: "
2018-
"(<class 'bool'>, <class 'numpy.bool_'>), as defined in the NXDL as NX_BOOLEAN."
2023+
f"(<class 'bool'>, <class '{np_bool}'>), as defined in the NXDL as NX_BOOLEAN."
20192024
],
20202025
id="string-instead-of-bool",
20212026
),

0 commit comments

Comments
 (0)