-
Notifications
You must be signed in to change notification settings - Fork 26
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
Remove enum_values keywords in serialization #565
Conversation
Auto-update pre-commit hooks
I'm concerned this feature might cause an issue for downstream packages. Often we use Enums as keys for dictionaries. This feature in monty seems like it will break this behaviour since dictionaries cannot be hashed and therefore cannot be used as keys. Have you tried running the atomate2 tests with this new branch of jobflow? |
@utf Thanks for your reply. I tried running the atomate2 tests locally and I can't see any issues arising when changing to my latest changes in monty and jobflow |
Thanks for checking @FabiPi3 |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #565 +/- ##
=======================================
Coverage 99.42% 99.42%
=======================================
Files 21 21
Lines 1564 1564
Branches 425 425
=======================================
Hits 1555 1555
Misses 9 9
|
In general, I would guess that your described use case is still covered. Looking into the actual implementation of
which means for me that only the values are serialized, the keys are still taken as strings. Hope this helps. |
Hi @FabiPi3, I am also thinking that there might some consequences to this change. To illustrate this I made an example with different objects and (de)serialization, including pydantic base models, that are often present in jobflow's output. Here is the code: from monty.json import MSONable, jsanitize
from enum import Enum
from pydantic import BaseModel
class E(Enum):
A = "A"
class ME(MSONable, Enum):
B = "B"
class BM1(BaseModel):
e: E = E.A
class BM2(BaseModel):
me: ME = ME.B
d = {"e": E.A, "me": ME.B}
print("Dictionary")
for obj in [{"e": E.A}, {"me": ME.B}]:
for enum_values in (True, False):
try:
print(f"{obj}, {enum_values}:", jsanitize(obj, strict=True, allow_bson=True, enum_values=enum_values))
except AttributeError:
print(f"{obj}, {enum_values}: failed")
print("\npydantic")
for cls in [BM1, BM2]:
for enum_values in (True, False):
try:
print(f"{cls}, {enum_values}:", jsanitize(cls(), strict=True, allow_bson=True, enum_values=enum_values))
try:
print(cls.model_validate(jsanitize(cls(), strict=True, allow_bson=True, enum_values=enum_values)))
except Exception as e:
print(f"{obj}, {enum_values}: pydantic failed: {e}")
except AttributeError:
print(f"{obj}, {enum_values}: jsanitize failed") Here is the output with the standard monty version:
Here is the output of your new monty version:
and just to hopefully make it clearer, here is the diff of the two outputs: 3c3
< {'e': <E.A: 'A'>}, False: failed
---
> {'e': <E.A: 'A'>}, False: {'e': {'value': 'A', '@module': '__main__', '@class': 'E', '@version': None}}
10c10,13
< {'me': <ME.B: 'B'>}, False: jsanitize failed
---
> <class '__main__.BM1'>, False: {'e': {'value': 'A', '@module': '__main__', '@class': 'E', '@version': None}, '@module': '__main__', '@class': 'BM1', '@version': None}
> {'me': <ME.B: 'B'>}, False: pydantic failed: 1 validation error for BM1
> e
> Input should be 'A' [type=enum, input_value={'value': 'A', '@module':...: 'E', '@version': None}, input_type=dict] Considering that you propose to pass from
In general there is definitely a benefit to this, but some issues may arise. I would also suggest to run the tests of emmet (and maybe maggma?) with the new monty version, to check that eveything works fine there as well. |
@gpetretto Thanks for your detailed answer. I cannot really judge how large those issues would be, but in my opinion everything should be fixable. Concerning the tests of emmet and maggma, everything that worked locally for me also worked with the new monty version. At least I couldn't see any issues arising there with the changes in monty. Hope this helps. |
The pull request in monty was accepted. We could go ahead and merge this now. If I understand, there are no really big issues preventing this. Please comment again about your opinion. Else I would close this (and adapt my application code). Thanks |
Instead of storing only the value of an Enum in the output, I suggest to store the whole enum class in order to be able to recreate it properly. This will be possible due to the new enum support in monty and after merging materialsvirtuallab/monty#640 this.