Skip to content

Commit

Permalink
Fix JSON serialiation of ValueEnums (#944)
Browse files Browse the repository at this point in the history
* Remove `ValueEnum`

* fix

* fix
  • Loading branch information
Andrew-S-Rosen authored Feb 21, 2024
1 parent 5fe2f18 commit 7735bb1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
7 changes: 0 additions & 7 deletions emmet-core/emmet/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
from typing import Any, Dict, Iterator, List, Optional, Union

import numpy as np

from monty.json import MSONable

from pydantic import BaseModel

from pymatgen.analysis.graphs import MoleculeGraph
from pymatgen.analysis.local_env import OpenBabelNN, metal_edge_extender
from pymatgen.analysis.molecule_matcher import MoleculeMatcher
Expand Down Expand Up @@ -307,10 +304,6 @@ def __hash__(self):
"""Get a hash of the enum."""
return hash(str(self))

def as_dict(self):
"""Create a serializable representation of the enum."""
return str(self.value)


class DocEnum(ValueEnum):
"""
Expand Down
2 changes: 1 addition & 1 deletion emmet-core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
include_package_data=True,
install_requires=[
"pymatgen>=2023.10.11",
"monty>=2023.9.25",
"monty>=2024.2.2",
"pydantic>=2.0",
"pydantic-settings>=2.0",
"pybtex~=0.24",
Expand Down
6 changes: 5 additions & 1 deletion emmet-core/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import datetime
import json
from pathlib import Path

import numpy as np
import pytest
from bson.objectid import ObjectId
from monty.json import MSONable
from monty.serialization import dumpfn

from emmet.core.utils import DocEnum, ValueEnum, jsanitize

Expand Down Expand Up @@ -68,13 +70,15 @@ def __eq__(self, other):
)


def test_value_enum():
def test_value_enum(monkeypatch, tmp_path):
class TempEnum(ValueEnum):
A = "A"
B = "B"

assert str(TempEnum.A) == "A"
assert str(TempEnum.B) == "B"
dumpfn(TempEnum, tmp_path / "temp.json")
assert Path(tmp_path, "temp.json").is_file()


def test_doc_enum():
Expand Down

0 comments on commit 7735bb1

Please sign in to comment.