17
17
PrivateAttr ,
18
18
PlainSerializer ,
19
19
BeforeValidator ,
20
- RootModel ,
21
20
)
22
- from typing import TYPE_CHECKING , Any , Annotated , TypeAlias
21
+ from typing import TYPE_CHECKING , Any , Annotated , TypeAlias , TypeVar
23
22
24
23
from pymatgen .core import Structure
25
24
from pymatgen .io .vasp .inputs import POTCAR_STATS_PATH , Incar , Kpoints , Poscar , Potcar , PmgVaspPspDirError
@@ -44,33 +43,28 @@ def _msonable_from_str(obj: Any, cls: type[MSONable]) -> MSONable:
44
43
return obj
45
44
46
45
46
+ IncarTypeVar = TypeVar ("IncarTypeVar" , Incar , str )
47
47
IncarType : TypeAlias = Annotated [
48
- Incar ,
48
+ IncarTypeVar ,
49
49
BeforeValidator (lambda x : _msonable_from_str (x , Incar )),
50
50
PlainSerializer (lambda x : json .dumps (x .as_dict ()), return_type = str ),
51
51
]
52
52
53
+ KpointsTypeVar = TypeVar ("KpointsTypeVar" , Kpoints , str )
53
54
KpointsType : TypeAlias = Annotated [
54
- Kpoints ,
55
+ KpointsTypeVar ,
55
56
BeforeValidator (lambda x : _msonable_from_str (x , Kpoints )),
56
57
PlainSerializer (lambda x : json .dumps (x .as_dict ()), return_type = str ),
57
58
]
58
59
60
+ StructureTypeVar = TypeVar ("StructureTypeVar" , Structure , str )
59
61
StructureType : TypeAlias = Annotated [
60
- Structure ,
62
+ StructureTypeVar ,
61
63
BeforeValidator (lambda x : _msonable_from_str (x , Structure )),
62
64
PlainSerializer (lambda x : json .dumps (x .as_dict ()), return_type = str ),
63
65
]
64
66
65
67
66
- class _MsonStrType (RootModel ):
67
- root : str
68
-
69
-
70
- for pmg_obj in (Incar , Kpoints , Structure ):
71
- setattr (pmg_obj , "__type_adapter__" , _MsonStrType )
72
-
73
-
74
68
class ValidationError (Exception ):
75
69
"""Define custom exception during validation."""
76
70
0 commit comments