Skip to content

Commit 08d8086

Browse files
slightly cleaner type annotations
1 parent 0b99e10 commit 08d8086

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

pymatgen/io/validation/common.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
PrivateAttr,
1818
PlainSerializer,
1919
BeforeValidator,
20-
RootModel,
2120
)
22-
from typing import TYPE_CHECKING, Any, Annotated, TypeAlias
21+
from typing import TYPE_CHECKING, Any, Annotated, TypeAlias, TypeVar
2322

2423
from pymatgen.core import Structure
2524
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:
4443
return obj
4544

4645

46+
IncarTypeVar = TypeVar("IncarTypeVar", Incar, str)
4747
IncarType: TypeAlias = Annotated[
48-
Incar,
48+
IncarTypeVar,
4949
BeforeValidator(lambda x: _msonable_from_str(x, Incar)),
5050
PlainSerializer(lambda x: json.dumps(x.as_dict()), return_type=str),
5151
]
5252

53+
KpointsTypeVar = TypeVar("KpointsTypeVar", Kpoints, str)
5354
KpointsType: TypeAlias = Annotated[
54-
Kpoints,
55+
KpointsTypeVar,
5556
BeforeValidator(lambda x: _msonable_from_str(x, Kpoints)),
5657
PlainSerializer(lambda x: json.dumps(x.as_dict()), return_type=str),
5758
]
5859

60+
StructureTypeVar = TypeVar("StructureTypeVar", Structure, str)
5961
StructureType: TypeAlias = Annotated[
60-
Structure,
62+
StructureTypeVar,
6163
BeforeValidator(lambda x: _msonable_from_str(x, Structure)),
6264
PlainSerializer(lambda x: json.dumps(x.as_dict()), return_type=str),
6365
]
6466

6567

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-
7468
class ValidationError(Exception):
7569
"""Define custom exception during validation."""
7670

0 commit comments

Comments
 (0)