From 104a83f472965040246450033f6cc5e8d5235d70 Mon Sep 17 00:00:00 2001 From: Andrew-S-Rosen Date: Wed, 21 Feb 2024 17:28:55 -0500 Subject: [PATCH 1/3] Remove `ValueEnum` --- emmet-core/emmet/core/symmetry.py | 4 ++-- emmet-core/emmet/core/utils.py | 27 +-------------------------- emmet-core/setup.py | 2 +- 3 files changed, 4 insertions(+), 29 deletions(-) diff --git a/emmet-core/emmet/core/symmetry.py b/emmet-core/emmet/core/symmetry.py index 4c27155efb..f1ca78396b 100644 --- a/emmet-core/emmet/core/symmetry.py +++ b/emmet-core/emmet/core/symmetry.py @@ -1,3 +1,4 @@ +from enum import Enum from typing import Any, Dict, Optional from pydantic import BaseModel, Field @@ -6,12 +7,11 @@ from pymatgen.symmetry.analyzer import PointGroupAnalyzer, SpacegroupAnalyzer, spglib from emmet.core.settings import EmmetSettings -from emmet.core.utils import ValueEnum SETTINGS = EmmetSettings() -class CrystalSystem(ValueEnum): +class CrystalSystem(Enum): """ The crystal system of the lattice """ diff --git a/emmet-core/emmet/core/utils.py b/emmet-core/emmet/core/utils.py index 53762d4c5d..2181c05b71 100644 --- a/emmet-core/emmet/core/utils.py +++ b/emmet-core/emmet/core/utils.py @@ -287,32 +287,7 @@ def jsanitize(obj, strict=False, allow_bson=False): return jsanitize(obj.as_dict(), strict=strict, allow_bson=allow_bson) -class ValueEnum(Enum): - """ - Enum that serializes to string as the value - """ - - def __str__(self): - return str(self.value) - - def __eq__(self, obj: object) -> bool: - """Special Equals to enable converting strings back to the enum""" - if isinstance(obj, str): - return super().__eq__(self.__class__(obj)) - elif isinstance(obj, self.__class__): - return super().__eq__(obj) - return False - - 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): +class DocEnum(Enum): """ Enum with docstrings support from: https://stackoverflow.com/a/50473952 diff --git a/emmet-core/setup.py b/emmet-core/setup.py index a0f65f5d40..6104899360 100644 --- a/emmet-core/setup.py +++ b/emmet-core/setup.py @@ -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", From de191e53dfd9a081520bacc6e39e41753d75a14e Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Wed, 21 Feb 2024 17:33:01 -0500 Subject: [PATCH 2/3] fix --- emmet-core/emmet/core/symmetry.py | 4 ++-- emmet-core/emmet/core/utils.py | 26 ++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/emmet-core/emmet/core/symmetry.py b/emmet-core/emmet/core/symmetry.py index f1ca78396b..4c27155efb 100644 --- a/emmet-core/emmet/core/symmetry.py +++ b/emmet-core/emmet/core/symmetry.py @@ -1,4 +1,3 @@ -from enum import Enum from typing import Any, Dict, Optional from pydantic import BaseModel, Field @@ -7,11 +6,12 @@ from pymatgen.symmetry.analyzer import PointGroupAnalyzer, SpacegroupAnalyzer, spglib from emmet.core.settings import EmmetSettings +from emmet.core.utils import ValueEnum SETTINGS = EmmetSettings() -class CrystalSystem(Enum): +class CrystalSystem(ValueEnum): """ The crystal system of the lattice """ diff --git a/emmet-core/emmet/core/utils.py b/emmet-core/emmet/core/utils.py index 2181c05b71..dedcb882e0 100644 --- a/emmet-core/emmet/core/utils.py +++ b/emmet-core/emmet/core/utils.py @@ -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 @@ -287,7 +284,28 @@ def jsanitize(obj, strict=False, allow_bson=False): return jsanitize(obj.as_dict(), strict=strict, allow_bson=allow_bson) -class DocEnum(Enum): +class ValueEnum(Enum): + """ + Enum that serializes to string as the value + """ + + def __str__(self): + return str(self.value) + + def __eq__(self, obj: object) -> bool: + """Special Equals to enable converting strings back to the enum""" + if isinstance(obj, str): + return super().__eq__(self.__class__(obj)) + elif isinstance(obj, self.__class__): + return super().__eq__(obj) + return False + + def __hash__(self): + """Get a hash of the enum.""" + return hash(str(self)) + + +class DocEnum(ValueEnum): """ Enum with docstrings support from: https://stackoverflow.com/a/50473952 From 2b9b9eba737861d559843a9a66deba95f0d89924 Mon Sep 17 00:00:00 2001 From: Andrew Rosen Date: Wed, 21 Feb 2024 17:35:03 -0500 Subject: [PATCH 3/3] fix --- emmet-core/tests/test_utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/emmet-core/tests/test_utils.py b/emmet-core/tests/test_utils.py index 6ef1bbccb6..68c7f2b657 100644 --- a/emmet-core/tests/test_utils.py +++ b/emmet-core/tests/test_utils.py @@ -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 @@ -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():