materialsproject / emmet

Be a master builder of databases of material properties. Avoid the Kragle.
https://materialsproject.github.io/emmet/
Other
51 stars 63 forks source link

Serialzation issues with ValueEnum #919

Closed jmmshn closed 5 months ago

jmmshn commented 6 months ago

I'm running into a few problems when serializing TaskDocs using dumpfn.

The following seems to be causing the problem:

https://github.com/materialsproject/emmet/blob/cc34208487060e41b7b428adee98d73669e2ad07/emmet-core/emmet/core/utils.py#L310C1-L313C1

When you call MontyEncoder().encode on a ValueEnum you end with d = "Hexagonal" entering this block of monty:

https://github.com/materialsvirtuallab/monty/blob/5d37679e1f0cd58a8d165d76f3a095cb71266831/monty/json.py#L417-L420

This leads to an error:

TypeError: 'str' object does not support item assignment

I think the right way to deal with this is to just make sure that monty understands enums which it kinda already does.

https://github.com/materialsvirtuallab/monty/blob/5d37679e1f0cd58a8d165d76f3a095cb71266831/monty/json.py#L194-L195

@munrojm, I can make that PR but I wanna make sure the effects are manageable first.

jmmshn commented 6 months ago

PS: I think jsanitize(..., enum_values=True) avoids the problem, but I still think having an as_dict that creates a str is problematic. Since Enums are fairly standard in python I think this should me taken care of in monty.