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

Fix JSON serialization of ValueEnums #944

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading