Skip to content

Commit c70e5b8

Browse files
authored
Add type annotations for io.vasp.outputs (materialsproject#3776)
* remove a lot of ignore tags * first go: quick look and comment/type tweaks * remove ALL type: ignore[reportPossiblyUnboundVariable] * a quick look * tweak module docstring * fix/supress PossiblyUnboundVariable * pre-commit fix * revert changes from other branch * [Need Discussion] set IVDW default to 0 * suppress None error for ElementTree searches * replace dict update with |= * fix typo in update -> |=, and mypy fixes * fix another typo in update -> |= * add DEBUG tag and mypy fixes * remove DEBUG tag * more mypy fixes * more mypy fixes * mypy fixes * finish mypy errors
1 parent cd8846b commit c70e5b8

File tree

7 files changed

+1095
-873
lines changed

7 files changed

+1095
-873
lines changed

pymatgen/core/trajectory.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from pymatgen.core.structure import Composition, DummySpecies, Element, Lattice, Molecule, Species, Structure
1818
from pymatgen.io.ase import AseAtomsAdaptor
19-
from pymatgen.io.vasp.outputs import Vasprun, Xdatcar
2019

2120
if TYPE_CHECKING:
2221
from collections.abc import Iterator
@@ -547,9 +546,13 @@ def from_file(cls, filename: str | Path, constant_lattice: bool = True, **kwargs
547546
structures = []
548547

549548
if fnmatch(filename, "*XDATCAR*"):
549+
from pymatgen.io.vasp.outputs import Xdatcar
550+
550551
structures = Xdatcar(filename).structures
551552

552553
elif fnmatch(filename, "vasprun*.xml*"):
554+
from pymatgen.io.vasp.outputs import Vasprun
555+
553556
structures = Vasprun(filename).structures
554557

555558
elif fnmatch(filename, "*.traj"):

pymatgen/entries/computed_entries.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def __init__(
291291
energy_adjustments: list | None = None,
292292
parameters: dict | None = None,
293293
data: dict | None = None,
294-
entry_id: object | None = None,
294+
entry_id: str | None = None,
295295
):
296296
"""Initialize a ComputedEntry.
297297
@@ -557,7 +557,7 @@ def __init__(
557557
energy_adjustments: list | None = None,
558558
parameters: dict | None = None,
559559
data: dict | None = None,
560-
entry_id: object | None = None,
560+
entry_id: str | None = None,
561561
) -> None:
562562
"""Initialize a ComputedStructureEntry.
563563
@@ -647,7 +647,10 @@ def from_dict(cls, dct: dict) -> Self:
647647
entry_id=dct.get("entry_id"),
648648
)
649649

650-
def normalize(self, mode: Literal["formula_unit", "atom"] = "formula_unit") -> ComputedStructureEntry:
650+
def normalize(
651+
self,
652+
mode: Literal["formula_unit", "atom"] = "formula_unit",
653+
) -> ComputedStructureEntry:
651654
"""Normalize the entry's composition and energy. The structure remains unchanged.
652655
653656
Args:
@@ -698,7 +701,7 @@ def __init__(
698701
energy_adjustments: list | None = None,
699702
parameters: dict | None = None,
700703
data: dict | None = None,
701-
entry_id: object | None = None,
704+
entry_id: str | None = None,
702705
):
703706
"""
704707
Args:

pymatgen/io/lobster/outputs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1380,14 +1380,14 @@ def __init__(
13801380
iband = 0
13811381
if line.split()[0] != "#":
13821382
if linenumber < self.nbands:
1383-
if ifilename == 0:
1383+
if ifilename == 0 and self.efermi is not None:
13841384
eigenvals[Spin.up][iband][idx_kpt] = float(line.split()[1]) + self.efermi
13851385

13861386
p_eigenvals[Spin.up][iband][idx_kpt][atom_names[ifilename]][orbital_names[ifilename]] = float(
13871387
line.split()[2]
13881388
)
13891389
if linenumber >= self.nbands and self.is_spinpolarized:
1390-
if ifilename == 0:
1390+
if ifilename == 0 and self.efermi is not None:
13911391
eigenvals[Spin.down][iband][idx_kpt] = float(line.split()[1]) + self.efermi
13921392
p_eigenvals[Spin.down][iband][idx_kpt][atom_names[ifilename]][orbital_names[ifilename]] = float(
13931393
line.split()[2]

pymatgen/io/vasp/optics.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,17 @@ class DielectricFunctionCalculator(MSONable):
7272

7373
@classmethod
7474
def from_vasp_objects(cls, vrun: Vasprun, waveder: Waveder) -> Self:
75-
"""Construct a DielectricFunction from Vasprun, Kpoint, and Waveder objects.
75+
"""Construct a DielectricFunction from Vasprun, Kpoint, and Waveder.
7676
7777
Args:
7878
vrun: Vasprun object
7979
kpoint: Kpoint object
8080
waveder: Waveder object
8181
"""
8282
bands = vrun.eigenvalues
83+
if bands is None:
84+
raise RuntimeError("eigenvalues cannot be None.")
85+
8386
sspins = [Spin.up, Spin.down]
8487
eigs = np.stack([bands[spin] for spin in sspins[: vrun.parameters["ISPIN"]]], axis=2)[..., 0]
8588
eigs = np.swapaxes(eigs, 0, 1)
@@ -95,6 +98,9 @@ def from_vasp_objects(cls, vrun: Vasprun, waveder: Waveder) -> Self:
9598
if vrun.parameters["ISYM"] != 0:
9699
raise NotImplementedError("ISYM != 0 is not implemented yet")
97100

101+
if efermi is None:
102+
raise ValueError("efermi cannot be None.")
103+
98104
return cls(
99105
cder_real=waveder.cder_real,
100106
cder_imag=waveder.cder_imag,

0 commit comments

Comments
 (0)