Skip to content

Commit cffccca

Browse files
fix: numpy scalars were serialized to a string, instead of a number
1 parent 11169c5 commit cffccca

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

solara/server/kernel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def json_default(obj):
5454
import numpy as np
5555

5656
if isinstance(obj, np.number):
57-
return repr(obj.item())
57+
return obj.item()
5858
else:
5959
raise TypeError("%r is not JSON serializable" % obj)
6060
else:

tests/unit/kernel_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import json
12
from datetime import datetime
23
from unittest.mock import Mock
34

45
from solara.server.kernel import SessionWebsocket
6+
import numpy as np
57

68

79
def test_session_datetime():
@@ -16,3 +18,20 @@ class Dummy:
1618
session.websockets.add(websocket)
1719
session.send(stream, {"msg_type": "test", "content": {"data": "test"}, "somedate": datetime.now()}) # type: ignore
1820
websocket.send.assert_called_once()
21+
22+
23+
def test_numpy_scalar():
24+
class Dummy:
25+
pass
26+
27+
websocket = Mock()
28+
stream = Dummy()
29+
stream.channel = "iopub" # type: ignore
30+
session = SessionWebsocket()
31+
session.websockets.add(websocket)
32+
v = np.int64(42)
33+
session.send(stream, {"msg_type": "test", "content": {"a_numpy_scalar": v}}) # type: ignore
34+
websocket.send.assert_called_once()
35+
json_string = websocket.send.call_args[0][0]
36+
json_data = json.loads(json_string)
37+
assert json_data["content"]["a_numpy_scalar"] == 42

0 commit comments

Comments
 (0)