Skip to content

Commit ad6eafe

Browse files
DanielYang59janosh
andauthored
Add type annotations for io.vasp.inputs/optics (#3740)
* some easy mypy fixes * ruff check pymatgen/io/vasp --select ANN204 --unsafe-fixes --fix * add type for io.vasp.help * add timeout 60 sec for requests.get * pre-commit auto-fixes * add timeout 60 sec for requests.get * fix default value of default_names * finish poscar.from_str * finish Poscar * finish Incar * temp save for potcarsingle * put dunder methods close and to the top * put properties close and to the top * put properties close and to the top * replace str with PathLike * add types for optics * suppress some overload * remove None type from completely untyped classes * pre-commit auto-fixes * fix type error outside io.vasp * check for None in Incar init * ruff fix * allow None * fix types * replace `defaultdict` with specific type * revert accidental changes * fix test * fix tests --------- Co-authored-by: Janosh Riebesell <[email protected]>
1 parent 666e1d7 commit ad6eafe

36 files changed

+949
-830
lines changed

dev_scripts/update_pt_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def gen_iupac_ordering():
234234
def add_electron_affinities():
235235
"""Update the periodic table data file with electron affinities."""
236236

237-
req = requests.get("https://wikipedia.org/wiki/Electron_affinity_(data_page)")
237+
req = requests.get("https://wikipedia.org/wiki/Electron_affinity_(data_page)", timeout=60)
238238
soup = BeautifulSoup(req.text, "html.parser")
239239
table = None
240240
for table in soup.find_all("table"):

pymatgen/alchemy/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def __init__(self, existing_structures, structure_matcher=None, symprec=None):
244244
structure matcher is used. A recommended value is 1e-5.
245245
"""
246246
self.symprec = symprec
247-
self.structure_list = []
247+
self.structure_list: list = []
248248
self.existing_structures = existing_structures
249249
if isinstance(structure_matcher, dict):
250250
self.structure_matcher = StructureMatcher.from_dict(structure_matcher)

pymatgen/alchemy/transmuters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def __init__(self, cif_string, transformations=None, primitive=True, extend_coll
244244
"""
245245
transformed_structures = []
246246
lines = cif_string.split("\n")
247-
structure_data = []
247+
structure_data: list = []
248248
read_data = False
249249
for line in lines:
250250
if re.match(r"^\s*data", line):

pymatgen/analysis/chemenv/coordination_environments/coordination_geometries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ def __init__(self, permutations_safe_override=False, only_symbols=None):
908908

909909
self.minpoints = {}
910910
self.maxpoints = {}
911-
self.separations_cg = {}
911+
self.separations_cg: dict[int, dict] = {}
912912
for cn in range(6, 21):
913913
for cg in self.get_implemented_geometries(coordination=cn):
914914
if only_symbols is not None and cg.ce_symbol not in only_symbols:

pymatgen/analysis/chemenv/coordination_environments/structure_environments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2085,7 +2085,7 @@ def __init__(self, coord_geoms=None):
20852085
coord_geoms: coordination geometries to be added to the chemical environment.
20862086
"""
20872087
if coord_geoms is None:
2088-
self.coord_geoms = {}
2088+
self.coord_geoms: dict = {}
20892089
else:
20902090
raise NotImplementedError(
20912091
"Constructor for ChemicalEnvironments with the coord_geoms argument is not yet implemented"

pymatgen/analysis/ewald.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ def __init__(self, matrix, m_list, num_to_return=1, algo=ALGO_FAST):
540540
if algo == EwaldMinimizer.ALGO_COMPLETE:
541541
raise NotImplementedError("Complete algo not yet implemented for EwaldMinimizer")
542542

543-
self._output_lists = []
543+
self._output_lists: list = []
544544
# Tag that the recurse function looks at each level. If a method
545545
# sets this to true it breaks the recursion and stops the search.
546546
self._finished = False

pymatgen/analysis/structure_prediction/substitution_probability.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(self, lambda_table=None, alpha=-5):
7979

8080
# create Z and px
8181
self.Z = 0
82-
self._px = defaultdict(float)
82+
self._px: dict[Species, float] = defaultdict(float)
8383
for s1, s2 in itertools.product(self.species, repeat=2):
8484
value = math.exp(self.get_lambda(s1, s2))
8585
self._px[s1] += value / 2

pymatgen/analysis/wulff.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ def __init__(self, normal, e_surf, normal_pt, dual_pt, index, m_ind_orig, miller
9090
self.index = index
9191
self.m_ind_orig = m_ind_orig
9292
self.miller = miller
93-
self.points = []
94-
self.outer_lines = []
93+
self.points: list = []
94+
self.outer_lines: list = []
9595

9696

9797
class WulffShape:

pymatgen/apps/battery/plotter.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
from __future__ import annotations
44

5+
from typing import TYPE_CHECKING
6+
57
import matplotlib.pyplot as plt
68
import plotly.graph_objects as go
79

810
from pymatgen.util.plotting import pretty_plot
911

12+
if TYPE_CHECKING:
13+
from pymatgen.apps.battery.battery_abc import AbstractElectrode
14+
1015
__author__ = "Shyue Ping Ong"
1116
__copyright__ = "Copyright 2012, The Materials Project"
1217
__version__ = "0.1"
@@ -18,7 +23,7 @@
1823
class VoltageProfilePlotter:
1924
"""A plotter to make voltage profile plots for batteries."""
2025

21-
def __init__(self, xaxis="capacity", hide_negative=False):
26+
def __init__(self, xaxis: str = "capacity", hide_negative: bool = False) -> None:
2227
"""
2328
Args:
2429
xaxis: The quantity to use as the xaxis. Can be either
@@ -28,11 +33,11 @@ def __init__(self, xaxis="capacity", hide_negative=False):
2833
- frac_x: the atomic fraction of the working ion
2934
hide_negative: If True only plot the voltage steps above zero.
3035
"""
31-
self._electrodes = {}
36+
self._electrodes: dict[str, AbstractElectrode] = {}
3237
self.xaxis = xaxis
3338
self.hide_negative = hide_negative
3439

35-
def add_electrode(self, electrode, label=None):
40+
def add_electrode(self, electrode: AbstractElectrode, label: str | None = None) -> None:
3641
"""Add an electrode to the plot.
3742
3843
Args:
@@ -41,11 +46,11 @@ def add_electrode(self, electrode, label=None):
4146
label: A label for the electrode. If None, defaults to a counting
4247
system, i.e. 'Electrode 1', 'Electrode 2', ...
4348
"""
44-
if not label:
49+
if label is None:
4550
label = f"Electrode {len(self._electrodes) + 1}"
4651
self._electrodes[label] = electrode
4752

48-
def get_plot_data(self, electrode, term_zero=True):
53+
def get_plot_data(self, electrode: AbstractElectrode, term_zero: bool = True) -> tuple[list, list]:
4954
"""
5055
Args:
5156
electrode: Electrode object
@@ -82,7 +87,7 @@ def get_plot_data(self, electrode, term_zero=True):
8287
y.append(0)
8388
return x, y
8489

85-
def get_plot(self, width=8, height=8, term_zero=True, ax: plt.Axes = None):
90+
def get_plot(self, width: float = 8, height: float = 8, term_zero: bool = True, ax: plt.Axes = None) -> plt.Axes:
8691
"""Returns a plot object.
8792
8893
Args:
@@ -112,12 +117,12 @@ def get_plot(self, width=8, height=8, term_zero=True, ax: plt.Axes = None):
112117

113118
def get_plotly_figure(
114119
self,
115-
width=800,
116-
height=600,
117-
font_dict=None,
118-
term_zero=True,
120+
width: float = 800,
121+
height: float = 600,
122+
font_dict: dict | None = None,
123+
term_zero: bool = True,
119124
**kwargs,
120-
):
125+
) -> plt.Figure:
121126
"""Return plotly Figure object.
122127
123128
Args:
@@ -163,28 +168,28 @@ def get_plotly_figure(
163168
fig.update_layout(template="plotly_white", title_x=0.5)
164169
return fig
165170

166-
def _choose_best_x_label(self, formula, work_ion_symbol):
171+
def _choose_best_x_label(self, formula: set[str], work_ion_symbol: set[str]) -> str:
167172
if self.xaxis in {"capacity", "capacity_grav"}:
168173
return "Capacity (mAh/g)"
169174
if self.xaxis == "capacity_vol":
170175
return "Capacity (Ah/l)"
171176

172-
formula = formula.pop() if len(formula) == 1 else None
177+
_formula: str | None = formula.pop() if len(formula) == 1 else None
173178

174-
work_ion_symbol = work_ion_symbol.pop() if len(work_ion_symbol) == 1 else None
179+
_work_ion_symbol: str | None = work_ion_symbol.pop() if len(work_ion_symbol) == 1 else None
175180

176181
if self.xaxis == "x_form":
177-
if formula and work_ion_symbol:
178-
return f"x in {work_ion_symbol}<sub>x</sub>{formula}"
182+
if _formula and _work_ion_symbol:
183+
return f"x in {_work_ion_symbol}<sub>x</sub>{_formula}"
179184
return "x Work Ion per Host F.U."
180185

181186
if self.xaxis == "frac_x":
182-
if work_ion_symbol:
183-
return f"Atomic Fraction of {work_ion_symbol}"
187+
if _work_ion_symbol:
188+
return f"Atomic Fraction of {_work_ion_symbol}"
184189
return "Atomic Fraction of Working Ion"
185190
raise RuntimeError("No xaxis label can be determined")
186191

187-
def show(self, width=8, height=6):
192+
def show(self, width: float = 8, height: float = 6) -> None:
188193
"""Show the voltage profile plot.
189194
190195
Args:

pymatgen/apps/borg/queen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, drone, rootpath=None, number_of_drones=1):
4444
"""
4545
self._drone = drone
4646
self._num_drones = number_of_drones
47-
self._data = []
47+
self._data: list = []
4848

4949
if rootpath:
5050
if number_of_drones > 1:

0 commit comments

Comments
 (0)