Skip to content

Commit 4f96ff5

Browse files
authored
Merge pull request #1377 from materialsproject/sgserde
Modify a couple of serdes
2 parents 1ae6903 + c65bc25 commit 4f96ff5

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

emmet-core/emmet/core/types/pymatgen_types/structure_graph_adapter.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import Annotated, TypeVar
1+
from typing import Annotated, TypeVar, NotRequired
22

3-
from pydantic import BeforeValidator, WrapSerializer
3+
from pydantic import BeforeValidator, WrapSerializer, PlainSerializer
44
from pymatgen.analysis.graphs import MoleculeGraph, StructureGraph
55
from typing_extensions import TypedDict
66

@@ -10,15 +10,30 @@
1010
pop_empty_structure_keys,
1111
)
1212

13-
GraphDescriptors = list[str, str] # type: ignore[type-arg]
13+
14+
class GraphDescriptorDict(TypedDict):
15+
name: str
16+
edge_weight_name: NotRequired[str]
17+
edge_weight_units: NotRequired[str]
18+
19+
20+
GraphDescriptors = Annotated[
21+
GraphDescriptorDict,
22+
BeforeValidator(
23+
lambda x: GraphDescriptorDict(
24+
**({row[0]: row[1] for row in x} if isinstance(x, list) else x)
25+
)
26+
),
27+
PlainSerializer(lambda x: list(x.items()) if isinstance(x, dict) else x),
28+
]
1429

1530

1631
class TypedNodeDict(TypedDict):
1732
id: int
1833

1934

2035
class TypedAdjacencyDict(TypedDict):
21-
to_jimage: list[int, int, int] # type: ignore[type-arg]
36+
to_jimage: tuple[int, int, int]
2237
weight: float
2338
id: int
2439
key: int
@@ -27,7 +42,7 @@ class TypedAdjacencyDict(TypedDict):
2742
class TypedGraphDict(TypedDict):
2843
directed: bool
2944
multigraph: bool
30-
graph: list[GraphDescriptors, GraphDescriptors, GraphDescriptors] # type: ignore[type-arg]
45+
graph: GraphDescriptors
3146
nodes: list[TypedNodeDict]
3247
adjacency: list[list[TypedAdjacencyDict]]
3348

emmet-core/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ all = [
4848
"seekpath>=2.0.1",
4949
"robocrys>=0.2.11",
5050
"matminer>=0.9.3",
51+
"scipy<1.17.0", # TODO: pending matminer fixes/deprecation
5152
"pymatgen-analysis-defects>=2024.7.18",
5253
"pymatgen-analysis-diffusion>=2024.7.15",
5354
"pymatgen-analysis-alloys>=0.0.6",

0 commit comments

Comments
 (0)