Skip to content

Commit 9cf718a

Browse files
committed
Add serialization fix
1 parent 56f6b2b commit 9cf718a

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/pytest_pl_grader/_student_code_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def student_function_temp() -> Any:
7272

7373
function_response: StudentFunctionResponse = {
7474
"status": "success" if execution_error is None else "exception",
75-
"value": result,
75+
"value": to_json(result),
7676
"stdout": stdout_capture.getvalue(),
7777
"stderr": stderr_capture.getvalue(),
7878
"exception_name": type(execution_error).__name__,

src/pytest_pl_grader/json_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ def to_json(v: Any) -> Any:
1313
"_concrete_type": type(v).__name__,
1414
"_value": str(v),
1515
}
16+
elif isinstance(v, np.bool_):
17+
return {
18+
"_type": "np_bool",
19+
"_value": str(v),
20+
}
1621
elif isinstance(v, np.ndarray):
1722
if np.isrealobj(v):
1823
return {"_type": "ndarray", "_value": v.tolist(), "_dtype": str(v.dtype)}
@@ -56,6 +61,11 @@ def from_json(v_json) -> Any:
5661
return complex(v_json["_value"]["real"], v_json["_value"]["imag"])
5762
else:
5863
raise ValueError("variable of type complex should have value with real and imaginary pair")
64+
elif v_json["_type"] == "np_bool":
65+
if "_value" in v_json:
66+
return np.bool_(v_json["_value"] == "True")
67+
else:
68+
raise ValueError("variable of type np_bool should have value")
5969
elif v_json["_type"] == "np_scalar":
6070
if "_concrete_type" in v_json and "_value" in v_json:
6171
return getattr(np, v_json["_concrete_type"])(v_json["_value"])

tests/test_serialize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_serialize_numpy_array() -> None:
2424
assert np.array_equal(arr, deserialized)
2525

2626

27-
@pytest.mark.parametrize("obj", [np.bool(True), np.int32(42), np.float64(3.14), complex(1, 2)])
27+
@pytest.mark.parametrize("obj", [np.bool(True), np.int32(42), np.float64(3.14), complex(1, 2), True])
2828
def test_serialize_json(obj: Any) -> None:
2929
# Serialize the object to JSON-compatible format
3030
json_compatible = to_json(obj)

0 commit comments

Comments
 (0)