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
44from pymatgen .analysis .graphs import MoleculeGraph , StructureGraph
55from typing_extensions import TypedDict
66
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
1631class TypedNodeDict (TypedDict ):
1732 id : int
1833
1934
2035class 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):
2742class 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
0 commit comments