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

JSON representation for MontyStore cannot be decoded with monty.json.MontyDecoder #826

Open
Andrew-S-Rosen opened this issue Jul 15, 2023 · 3 comments
Labels

Comments

@Andrew-S-Rosen
Copy link
Member

Andrew-S-Rosen commented Jul 15, 2023

Overview

Given a Store.to_json() representation, one would ideally be able to use MontyDecoder to regenerate the Store object. This works for MemoryStore, MongoStore, etc. but does not work for MontyStore for reasons I have not yet understood.

Sidenote: very confusing here that MontyStore refers to MontyDB but MontyDecoder is the monty package 😅

Example

from maggma.stores import MontyStore
from monty.json import MontyDecoder
store = MontyStore("test")
store_json = store.to_json()
store_remade = MontyDecoder().decode(store_json)
      3 store = MontyStore("test")
      4 store_json = store.to_json()
----> 5 store_remade = MontyDecoder().decode(store_json)

File ~/software/miniconda/envs/quacc/lib/python3.10/site-packages/monty/json.py:497, in
 MontyDecoder.decode(self, s)
    495 else:
    496     d = json.loads(s)
--> 497 return self.process_decoded(d)

File ~/software/miniconda/envs/quacc/lib/python3.10/site-packages/monty/json.py:444, in
 MontyDecoder.process_decoded(self, d)
    442 if hasattr(cls_, "from_dict"):
    443     return cls_.from_dict(data)
--> 444 if pydantic is not None and issubclass(cls_, pydantic.BaseModel):  # pylint: disable=E1101
    445     return cls_(**data)
    446 if (
    447     dataclasses is not None
    448     and (not issubclass(cls_, MSONable))
    449     and dataclasses.is_dataclass(cls_)
    450 ):

File ~/software/miniconda/envs/quacc/lib/python3.10/abc.py:123, in ABCMeta.__subclasscheck__(cls, subclass)
    121 def __subclasscheck__(cls, subclass):
    122     """Override for issubclass(subclass, cls)."""
--> 123     return _abc_subclasscheck(cls, subclass)

TypeError: issubclass() arg 1 must be a class

Alternatives

I also tried MontyDecoder().process_decoded(store.as_dict()), but that led to the same error.

@Andrew-S-Rosen Andrew-S-Rosen changed the title JSON representation for MontyDecoder cannot be decoded JSON representation for MontyStore cannot be decoded with MontyDecoder Jul 15, 2023
@Andrew-S-Rosen Andrew-S-Rosen changed the title JSON representation for MontyStore cannot be decoded with MontyDecoder JSON representation for MontyStore cannot be decoded with monty.json.MontyDecoder Jul 15, 2023
@rkingsbury
Copy link
Collaborator

@arosen93 is this still an issue? I see it was closed and then re-opened (and also note the discussion in #825 )

If you think there's some use case for a as_dict / from_dict method for StoreI'd be happy to discuss

@Andrew-S-Rosen
Copy link
Member Author

Andrew-S-Rosen commented Jul 21, 2023

This is indeed still an issue! I closed it by mistake, and this was after trying @mkhorton's recommendation, which works for most stores except MontyStore in particular.

Unfortunately, I haven't had time to dig into why. This isn't a dealbreaker for me or anything, but I wanted to report it nonetheless since it's unexpected behavior.

@rkingsbury rkingsbury added the bug label Jul 31, 2023
@Andrew-S-Rosen
Copy link
Member Author

Andrew-S-Rosen commented Feb 10, 2024

The underlying reason is because MontyStore is a function, not a class. It only becomes a class when instantiated. This causes some issues with (de)serialization.

from maggma.stores import MontyStore

print(MontyStore) # function
print(MontyStore('test')) # class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants