Skip to content

Commit

Permalink
Merge pull request #671 from Andrew-S-Rosen/patch-5
Browse files Browse the repository at this point in the history
Fix removal of `recursive_msonable` keyword argument in internal `jsanitize` calls
  • Loading branch information
shyuep committed May 13, 2024
2 parents 1041b17 + 8dcda56 commit e80685e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
25 changes: 20 additions & 5 deletions monty/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,14 +895,29 @@ def jsanitize(
return obj
if isinstance(obj, (list, tuple)):
return [
jsanitize(i, strict=strict, allow_bson=allow_bson, enum_values=enum_values)
jsanitize(
i,
strict=strict,
allow_bson=allow_bson,
enum_values=enum_values,
recursive_msonable=recursive_msonable,
)
for i in obj
]
if np is not None and isinstance(obj, np.ndarray):
return [
jsanitize(i, strict=strict, allow_bson=allow_bson, enum_values=enum_values)
for i in obj.tolist()
]
try:
return [
jsanitize(
i,
strict=strict,
allow_bson=allow_bson,
enum_values=enum_values,
recursive_msonable=recursive_msonable,
)
for i in obj.tolist()
]
except TypeError:
return obj.tolist()
if np is not None and isinstance(obj, np.generic):
return obj.item()
if _check_type(
Expand Down
4 changes: 4 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ def test_numpy(self):
d = jsanitize(x, strict=True)
assert isinstance(d["energies"][0], float)

x = {"energy": np.array(-1.0)}
d = jsanitize(x, strict=True)
assert isinstance(d["energy"], float)

# Test data nested in a class
x = np.array([[1 + 1j, 2 + 1j], [3 + 1j, 4 + 1j]], dtype="complex64")
cls = ClassContainingNumpyArray(np_a={"a": [{"b": x}]})
Expand Down

0 comments on commit e80685e

Please sign in to comment.