Skip to content
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

fix(numpy): remove np.float_ and np.complex_ for compatibility with numpy 2.0 #28

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions trame_client/encoders/numpy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import json

import numpy as np

np_version = tuple(map(int, np.__version__.split(".")))
if np_version < (2, 0, 0):
NP_FLOATS = (np.float_, np.float16, np.float32, np.float64)
NP_COMPLEX = (np.complex_, np.complex64, np.complex128)
else:
NP_FLOATS = (np.float16, np.float32, np.float64)
NP_COMPLEX = (np.complex64, np.complex128)


class NumpyEncoder(json.JSONEncoder):
"""Custom encoder for numpy data types"""
Expand All @@ -24,10 +33,10 @@ def default(self, obj):
):
return int(obj)

elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
elif isinstance(obj, NP_FLOATS):
return float(obj)

elif isinstance(obj, (np.complex_, np.complex64, np.complex128)):
elif isinstance(obj, NP_COMPLEX):
return {"real": obj.real, "imag": obj.imag}

elif isinstance(obj, (np.ndarray,)):
Expand Down
Loading