Skip to content

Commit 376aa81

Browse files
authored
Merge pull request #1438 from compas-dev/fix-numpy2-float-import
Fix numpy error
2 parents 9de7875 + 566dc03 commit 376aa81

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818

1919
* Fixed publish to YAK via CI workflow.
2020
* Added selector for `test` and `prod` to CI workflow.
21+
* Fixed `AttributeError` in `compas.data.DataEncoder.default` due to `np.float_` no longer being available in `numpy>=2`.
2122

2223
### Removed
2324

src/compas/data/encoders.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
try:
3131
import numpy as np
3232

33+
try:
34+
np_float = np.float_
35+
except AttributeError:
36+
np_float = np.float64
37+
3338
numpy_support = True
3439
except (ImportError, SyntaxError):
3540
numpy_support = False
@@ -143,7 +148,7 @@ def default(self, o):
143148
), # type: ignore
144149
):
145150
return int(o)
146-
if isinstance(o, (np.float_, np.float16, np.float32, np.float64)): # type: ignore
151+
if isinstance(o, (np_float, np.float16, np.float32, np.float64)): # type: ignore
147152
return float(o)
148153
if isinstance(o, np.bool_):
149154
return bool(o)

tests/compas/data/test_json_numpy.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,24 @@ def test_json_numpy():
1515

1616
except ImportError:
1717
pass
18+
19+
20+
try:
21+
import numpy as np
22+
23+
try:
24+
np_float = np.float_
25+
except AttributeError:
26+
np_float = np.float64
27+
28+
def test_json_numpy_float():
29+
before = [
30+
np.array([1, 2, 3], dtype=np_float),
31+
np.array([1.0, 2.0, 3.0]),
32+
np_float(1.0),
33+
]
34+
after = compas.json_loads(compas.json_dumps(before))
35+
assert after == [[1, 2, 3], [1.0, 2.0, 3.0], 1.0]
36+
37+
except ImportError:
38+
pass

0 commit comments

Comments
 (0)