Skip to content

Commit

Permalink
assert_msonable replace obj.to_json() with json.dumps(obj.as_dict(), …
Browse files Browse the repository at this point in the history
…cls=MontyEncoder)
  • Loading branch information
janosh committed Apr 10, 2024
1 parent 30be59a commit e023eec
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pymatgen/io/abinit/abiobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def as_dict(self):
@classmethod
def from_dict(cls, dct: dict) -> Self:
"""Build object from dict."""
return cls(**{k: dct[k] for k in dct if k in cls._fields})
return cls(**{key: dct[key] for key in dct if key in cls._fields})


# An handy Multiton
Expand Down
4 changes: 2 additions & 2 deletions pymatgen/util/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from unittest import TestCase

import pytest
from monty.json import MontyDecoder, MSONable
from monty.json import MontyDecoder, MontyEncoder, MSONable
from monty.serialization import loadfn

from pymatgen.core import ROOT, SETTINGS, Structure
Expand Down Expand Up @@ -141,7 +141,7 @@ def assert_msonable(self, obj: MSONable, test_is_subclass: bool = True) -> str:
if test_is_subclass:
assert isinstance(obj, MSONable)
assert obj.as_dict() == type(obj).from_dict(obj.as_dict()).as_dict()
json_str = obj.to_json()
json_str = json.dumps(obj.as_dict(), cls=MontyEncoder)
round_trip = json.loads(json_str, cls=MontyDecoder)
assert issubclass(type(round_trip), type(obj)), f"{type(round_trip)} != {type(obj)}"
return json_str

0 comments on commit e023eec

Please sign in to comment.