Skip to content

Commit 92b1cfb

Browse files
committed
More typing fixes.
1 parent 031169b commit 92b1cfb

30 files changed

+92
-60
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,12 @@ exclude_also = [
301301
ignore_missing_imports = true
302302
namespace_packages = true
303303
no_implicit_optional = false
304-
disable_error_code = ["annotation-unchecked", "override", "operator", "attr-defined", "union-attr", "arg-type"] #, "operator", "arg-type", "index", "call-arg", "return-value", "assignment", "attr-defined"]
304+
disable_error_code = ["annotation-unchecked", "override", "operator", "attr-defined", "union-attr"] #, "operator", "arg-type", "index", "call-arg", "return-value", "assignment", "attr-defined"]
305305
exclude = ['src/pymatgen/analysis', 'src/pymatgen/io', 'src/pymatgen/cli', 'src/pymatgen/electronic_structure', 'src/pymatgen/phonon']
306306
plugins = ["numpy.typing.mypy_plugin"]
307307

308308
[[tool.mypy.overrides]]
309-
module = ["requests.*", "tabulate.*"]
309+
module = ["requests.*", "tabulate.*", "monty.*"]
310310
ignore_missing_imports = true
311311

312312
[tool.codespell]

src/pymatgen/command_line/bader_caller.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
from typing_extensions import Self
3939

40-
from pymatgen.core import Structure
40+
from pymatgen.core import IStructure, Structure
4141

4242
__author__ = "shyuepingong"
4343
__version__ = "0.1"
@@ -351,7 +351,7 @@ def get_partial_charge(self, atom_index: int, nelect: int | None = None) -> floa
351351
"""
352352
return -self.get_charge_transfer(atom_index, nelect)
353353

354-
def get_charge_decorated_structure(self) -> Structure:
354+
def get_charge_decorated_structure(self) -> Structure | IStructure:
355355
"""Get a charge decorated structure.
356356
357357
Note, this assumes that the Bader analysis was correctly performed on a file

src/pymatgen/core/ion.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ def get_reduced_formula_and_factor(
173173
comp = self.composition - nH2O * Composition("H2O")
174174

175175
el_amt_dict = {k: round(v) for k, v in comp.get_el_amt_dict().items()}
176-
formula, factor = reduce_formula(el_amt_dict, iupac_ordering=iupac_ordering)
176+
formula, factor_int = reduce_formula(el_amt_dict, iupac_ordering=iupac_ordering)
177+
factor = float(factor_int)
177178

178179
# This line checks specifically that the contains an equal amount of O and H. When that is the case,
179180
# they should be displayed as "OH" rather than "HO".

src/pymatgen/core/periodic_table.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,6 @@ def __init__(
10561056
ValueError: If oxidation state passed both in symbol string and via
10571057
oxidation_state kwarg.
10581058
"""
1059-
symbol = str(symbol) # First ensure that any symbol is converted to a string.
10601059
if oxidation_state is not None and symbol.endswith(("+", "-")):
10611060
raise ValueError(
10621061
f"Oxidation state should be specified either in {symbol=} or as {oxidation_state=}, not both."

src/pymatgen/core/structure.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,7 +2961,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
29612961
json_str = json.dumps(self.as_dict(), **kwargs)
29622962
if filename:
29632963
with zopen(filename, mode="wt", encoding="utf-8") as file:
2964-
file.write(json_str)
2964+
file.write(json_str) # type:ignore[arg-type]
29652965
return json_str
29662966

29672967
elif fmt == "xsf" or fnmatch(filename.lower(), "*.xsf*"):
@@ -2970,7 +2970,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
29702970
res_str = XSF(self).to_str()
29712971
if filename:
29722972
with zopen(filename, mode="wt", encoding="utf-8") as file:
2973-
file.write(res_str)
2973+
file.write(res_str) # type:ignore[arg-type]
29742974
return res_str
29752975

29762976
elif (
@@ -2984,7 +2984,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
29842984
res_str = Mcsqs(self).to_str()
29852985
if filename:
29862986
with zopen(filename, mode="wt", encoding="ascii") as file:
2987-
file.write(res_str)
2987+
file.write(res_str) # type:ignore[arg-type]
29882988
return res_str
29892989

29902990
elif fmt == "prismatic" or fnmatch(filename, "*prismatic*"):
@@ -2999,7 +2999,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
29992999
yaml_str = str_io.getvalue()
30003000
if filename:
30013001
with zopen(filename, mode="wt", encoding="utf-8") as file:
3002-
file.write(yaml_str)
3002+
file.write(yaml_str) # type:ignore[arg-type]
30033003
return yaml_str
30043004

30053005
elif fmt == "aims" or fnmatch(filename, "geometry.in"):
@@ -3008,9 +3008,9 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
30083008
geom_in = AimsGeometryIn.from_structure(self)
30093009
if filename:
30103010
with zopen(filename, mode="wt", encoding="utf-8") as file:
3011-
file.write(geom_in.get_header(filename))
3012-
file.write(geom_in.content)
3013-
file.write("\n")
3011+
file.write(geom_in.get_header(filename)) # type:ignore[arg-type]
3012+
file.write(geom_in.content) # type:ignore[arg-type]
3013+
file.write("\n") # type:ignore[arg-type]
30143014
return geom_in.content
30153015

30163016
# fleur support implemented in external namespace pkg https://github.com/JuDFTteam/pymatgen-io-fleur
@@ -3025,7 +3025,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
30253025
res_str = ResIO.structure_to_str(self)
30263026
if filename:
30273027
with zopen(filename, mode="wt", encoding="utf-8") as file:
3028-
file.write(res_str)
3028+
file.write(res_str) # type:ignore[arg-type]
30293029
return res_str
30303030

30313031
elif fmt == "pwmat" or fnmatch(filename.lower(), "*.pwmat") or fnmatch(filename.lower(), "*.config"):
@@ -4013,7 +4013,7 @@ def to(self, filename: str = "", fmt: str = "") -> str | None:
40134013
json_str = json.dumps(self.as_dict())
40144014
if filename:
40154015
with zopen(filename, mode="wt", encoding="utf-8") as file:
4016-
file.write(json_str.encode("utf-8"))
4016+
file.write(json_str) # type:ignore[arg-type]
40174017
return json_str
40184018
elif fmt in {"yaml", "yml"} or fnmatch(filename, "*.yaml*") or fnmatch(filename, "*.yml*"):
40194019
yaml = YAML()
@@ -4022,7 +4022,7 @@ def to(self, filename: str = "", fmt: str = "") -> str | None:
40224022
yaml_str = str_io.getvalue()
40234023
if filename:
40244024
with zopen(filename, mode="wt", encoding="utf-8") as file:
4025-
file.write(yaml_str.encode("utf-8"))
4025+
file.write(yaml_str) # type:ignore[arg-type]
40264026
return yaml_str
40274027
else:
40284028
from pymatgen.io.babel import BabelMolAdaptor

src/pymatgen/core/trajectory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ def write_Xdatcar(
475475
xdatcar_str = "\n".join(lines) + "\n"
476476

477477
with zopen(filename, mode="wt", encoding="utf-8") as file:
478-
file.write(xdatcar_str)
478+
file.write(xdatcar_str) # type:ignore[arg-type]
479479

480480
def as_dict(self) -> dict:
481481
"""Return the trajectory as a MSONable dict."""

src/pymatgen/entries/compatibility.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ def __init__(
773773
self.run_types = run_types
774774
super().__init__()
775775

776-
def get_adjustments(self, entry: AnyComputedEntry) -> list[EnergyAdjustment]:
776+
def get_adjustments(self, entry: AnyComputedEntry) -> list[ConstantEnergyAdjustment]:
777777
"""Get the list of energy adjustments to be applied to an entry."""
778778
adjustment_list = []
779779
if entry.parameters.get("run_type") not in self.run_types:
@@ -1053,7 +1053,7 @@ def __init__(
10531053
self.u_corrections = {}
10541054
self.u_errors = {}
10551055

1056-
def get_adjustments(self, entry: AnyComputedEntry) -> list[EnergyAdjustment]:
1056+
def get_adjustments(self, entry: AnyComputedEntry) -> list[CompositionEnergyAdjustment]:
10571057
"""Get the energy adjustments for a ComputedEntry or ComputedStructureEntry.
10581058
10591059
Energy corrections are implemented directly in this method instead of in
@@ -1439,7 +1439,7 @@ def __init__(
14391439
self.name = "MP Aqueous free energy adjustment"
14401440
super().__init__()
14411441

1442-
def get_adjustments(self, entry: ComputedEntry) -> list[EnergyAdjustment]:
1442+
def get_adjustments(self, entry: ComputedEntry) -> list[TemperatureEnergyAdjustment]:
14431443
"""Get the corrections applied to a particular entry.
14441444
14451445
Args:

src/pymatgen/io/abinit/abiobjects.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
from typing_extensions import Self
2222

23+
from pymatgen.core.structure import IStructure
24+
2325

2426
def lattice_from_abivars(cls=None, *args, **kwargs):
2527
"""Get a `Lattice` object from a dictionary with the Abinit variables `acell`
@@ -185,7 +187,7 @@ def species_by_znucl(structure: Structure) -> list[Species]:
185187

186188

187189
def structure_to_abivars(
188-
structure: Structure,
190+
structure: Structure | IStructure,
189191
enforce_znucl: list | None = None,
190192
enforce_typat: list | None = None,
191193
**kwargs,

src/pymatgen/io/aims/sets/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
if TYPE_CHECKING:
2121
from collections.abc import Iterable, Sequence
2222

23+
from pymatgen.core.structure import IStructure
2324
from pymatgen.util.typing import PathLike
2425

2526
TMPDIR_NAME: str = "tmpdir"
@@ -374,7 +375,7 @@ def get_parameter_updates(
374375

375376
def d2k(
376377
self,
377-
structure: Structure,
378+
structure: Structure | IStructure,
378379
kpt_density: float | tuple[float, float, float] = 5.0,
379380
even: bool = True,
380381
) -> Iterable[float]:
@@ -395,7 +396,7 @@ def d2k(
395396
recip_cell = structure.lattice.inv_matrix.transpose()
396397
return self.d2k_recip_cell(recip_cell, structure.lattice.pbc, kpt_density, even)
397398

398-
def k2d(self, structure: Structure, k_grid: np.ndarray[int]):
399+
def k2d(self, structure: Structure | IStructure, k_grid: np.ndarray[int]):
399400
"""Generate the kpoint density in each direction from given k_grid.
400401
401402
Args:

src/pymatgen/io/aims/sets/bs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
from collections.abc import Sequence
1414
from typing import Any
1515

16+
from pymatgen.core.structure import IStructure
17+
1618

1719
class _SegmentDict(TypedDict):
1820
coords: list[list[float]]
1921
labels: list[str]
2022
length: int
2123

2224

23-
def prepare_band_input(structure: Structure, density: float = 20):
25+
def prepare_band_input(structure: Structure | IStructure, density: float = 20):
2426
"""Prepare the band information needed for the FHI-aims control.in file.
2527
2628
Args:

0 commit comments

Comments
 (0)