From 7e499adf6e6446446f9a12eaf55c8a772c16fcd6 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 13:09:34 -0700 Subject: [PATCH 01/27] thermo - improved type hints for sort func --- emmet-core/emmet/core/thermo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emmet-core/emmet/core/thermo.py b/emmet-core/emmet/core/thermo.py index e1ff775da3..1e8dcf225d 100644 --- a/emmet-core/emmet/core/thermo.py +++ b/emmet-core/emmet/core/thermo.py @@ -152,7 +152,7 @@ def from_entries( entry_quality_scores = {"GGA": 1, "GGA+U": 2, "SCAN": 3, "R2SCAN": 4} - def _energy_eval(entry: ComputedStructureEntry): + def _energy_eval(entry: Union[ComputedStructureEntry, ComputedEntry]): """ Helper function to order entries for thermo energy data selection - Run type From 68cd333be2fc1d7868e3b4ffc0c01dddf63cfebd Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 13:14:14 -0700 Subject: [PATCH 02/27] thero mypy ignore directives --- emmet-core/emmet/core/thermo.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/emmet-core/emmet/core/thermo.py b/emmet-core/emmet/core/thermo.py index 1e8dcf225d..f3cef9fbd3 100644 --- a/emmet-core/emmet/core/thermo.py +++ b/emmet-core/emmet/core/thermo.py @@ -177,7 +177,7 @@ def _energy_eval(entry: Union[ComputedStructureEntry, ComputedEntry]): blessed_entry = sorted_entries[0] - (decomp, ehull) = pd.get_decomp_and_e_above_hull(blessed_entry) + (decomp, ehull) = pd.get_decomp_and_e_above_hull(blessed_entry) # type: ignore[arg-type] builder_meta = EmmetMeta(license=blessed_entry.data.get("license")) @@ -189,7 +189,7 @@ def _energy_eval(entry: Union[ComputedStructureEntry, ComputedEntry]): / blessed_entry.composition.num_atoms, "energy_per_atom": blessed_entry.energy / blessed_entry.composition.num_atoms, - "formation_energy_per_atom": pd.get_form_energy_per_atom(blessed_entry), + "formation_energy_per_atom": pd.get_form_energy_per_atom(blessed_entry), # type: ignore[arg-type] "energy_above_hull": ehull, "is_stable": blessed_entry in pd.stable_entries, "builder_meta": builder_meta.model_dump(), @@ -201,31 +201,31 @@ def _energy_eval(entry: Union[ComputedStructureEntry, ComputedEntry]): # Store different info if stable vs decomposes if d["is_stable"]: - d[ - "equilibrium_reaction_energy_per_atom" - ] = pd.get_equilibrium_reaction_energy(blessed_entry) + d["equilibrium_reaction_energy_per_atom"] = ( + pd.get_equilibrium_reaction_energy(blessed_entry) # type: ignore[arg-type] + ) else: d["decomposes_to"] = [ { - "material_id": de.data["material_id"], + "material_id": de.data["material_id"], # type: ignore[union-attr] "formula": de.composition.formula, "amount": amt, } - for de, amt in decomp.items() + for de, amt in decomp.items() # type: ignore[union-attr] ] try: decomp, energy = pd.get_decomp_and_phase_separation_energy( - blessed_entry + blessed_entry # type: ignore[arg-type] ) d["decomposition_enthalpy"] = energy d["decomposition_enthalpy_decomposes_to"] = [ { - "material_id": de.data["material_id"], + "material_id": de.data["material_id"], # type: ignore[union-attr] "formula": de.composition.formula, "amount": amt, } - for de, amt in decomp.items() + for de, amt in decomp.items() # type: ignore[union-attr] ] except ValueError: # try/except so this quantity does not take down the builder if it fails: @@ -254,7 +254,7 @@ def _energy_eval(entry: Union[ComputedStructureEntry, ComputedEntry]): docs.append( ThermoDoc.from_structure( - meta_structure=blessed_entry.structure, **d, **kwargs + meta_structure=blessed_entry.structure, **d, **kwargs # type: ignore[attr-defined] ) ) From 378b60b2019fd6d2fd08cf1a83f238c4f09ac7d5 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 13:18:16 -0700 Subject: [PATCH 03/27] return type hinting for feff/task.py --- emmet-core/emmet/core/feff/task.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/emmet-core/emmet/core/feff/task.py b/emmet-core/emmet/core/feff/task.py index ab9881ab02..ab94ae5b6d 100644 --- a/emmet-core/emmet/core/feff/task.py +++ b/emmet-core/emmet/core/feff/task.py @@ -4,7 +4,7 @@ from pydantic import Field from pymatgen.analysis.xas.spectrum import XAS from pymatgen.core import Structure -from pymatgen.core.periodic_table import Element +from pymatgen.core.periodic_table import Element, Species from emmet.core.structure import StructureMetadata from emmet.core.vasp.task_valid import TaskDocument as BaseTaskDocument @@ -48,7 +48,7 @@ class TaskDocument(BaseTaskDocument, StructureMetadata): # TEMP Stub properties for compatibility with atomate drone @property - def absorbing_element(self) -> Element: + def absorbing_element(self) -> Element | Species: if isinstance(self.structure[self.absorbing_atom].specie, Element): return self.structure[self.absorbing_atom].specie return self.structure[self.absorbing_atom].specie.element From 2cba3f3c7ae2bf016e85837ddeb111c646eeee52 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 14:53:47 -0700 Subject: [PATCH 04/27] return type hinting for structure_group --- emmet-core/emmet/core/structure_group.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emmet-core/emmet/core/structure_group.py b/emmet-core/emmet/core/structure_group.py index 5f62b048cc..ee4b774e75 100644 --- a/emmet-core/emmet/core/structure_group.py +++ b/emmet-core/emmet/core/structure_group.py @@ -248,7 +248,7 @@ def group_entries_with_structure_matcher( g, struct_matcher: StructureMatcher, working_ion: Optional[str] = None, -) -> Iterable[List[Union[ComputedStructureEntry]]]: +) -> Iterable[List[Union[ComputedStructureEntry, ComputedEntry]]]: """ Group the entries together based on similarity of the primitive cells Args: From ef76822ea949d065925960d54e63c190e0b02465 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 15:09:56 -0700 Subject: [PATCH 05/27] type hinting for migrationgraph --- emmet-core/emmet/core/mobility/migrationgraph.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/emmet-core/emmet/core/mobility/migrationgraph.py b/emmet-core/emmet/core/mobility/migrationgraph.py index ad968501a3..6305a1e786 100644 --- a/emmet-core/emmet/core/mobility/migrationgraph.py +++ b/emmet-core/emmet/core/mobility/migrationgraph.py @@ -193,7 +193,7 @@ def generate_sc_fields( min_length=min_length_sc, ) - sc_mat = sc_mat.tolist() + sc_mat = sc_mat.tolist() # type: ignore[attr-defined] host_sc = mg.host_structure * sc_mat working_ion = mg.only_sites[0].species_string @@ -205,9 +205,7 @@ def generate_sc_fields( return host_sc, sc_mat, min_length_sc, minmax_num_atoms, coords_list, combo @staticmethod - def ordered_sc_site_list( - uc_sites_only: Structure, sc_mat: List[List[Union[int, float]]] - ): + def ordered_sc_site_list(uc_sites_only: Structure, sc_mat: List[List[int]]): uc_no_site = uc_sites_only.copy() uc_no_site.remove_sites(range(len(uc_sites_only))) working_ion = uc_sites_only[0].species_string @@ -238,7 +236,7 @@ def ordered_sc_site_list( @staticmethod def get_hop_sc_combo( unique_hops: Dict, - sc_mat: List[List[Union[int, float]]], + sc_mat: List[List[int]], sm: StructureMatcher, host_sc: Structure, working_ion: str, @@ -321,7 +319,7 @@ def append_new_site( host_sc: Structure, ordered_sc_site_list: list, one_hop: Dict, - sc_mat: List[List[Union[int, float]]], + sc_mat: List[List[int]], working_ion: str, ): sc_mat_inv = np.linalg.inv(sc_mat) From 17e28ac9a5bed3ee9d08e69c635857896c3bdecb Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 15:23:44 -0700 Subject: [PATCH 06/27] mypy ignore directive for alloy_pairs assignment --- emmet-core/emmet/core/alloys.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emmet-core/emmet/core/alloys.py b/emmet-core/emmet/core/alloys.py index b9b333a1c7..06063ce226 100644 --- a/emmet-core/emmet/core/alloys.py +++ b/emmet-core/emmet/core/alloys.py @@ -33,5 +33,5 @@ class AlloySystemDoc(EmmetBaseModel): @classmethod def from_pair(cls, alloy_system: AlloySystem): # Too large to duplicate alloy pairs here. - alloy_system.alloy_pairs = None + alloy_system.alloy_pairs = None # type: ignore[assignment] return cls(alloy_system=alloy_system, alloy_id=alloy_system.alloy_id) From 329456507fed25ec419a8a39e80532006f23e971 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 15:33:19 -0700 Subject: [PATCH 07/27] mypy ignore directive for chemenv --- emmet-core/emmet/core/chemenv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emmet-core/emmet/core/chemenv.py b/emmet-core/emmet/core/chemenv.py index daf11967ea..b70baf7194 100644 --- a/emmet-core/emmet/core/chemenv.py +++ b/emmet-core/emmet/core/chemenv.py @@ -442,7 +442,7 @@ def from_structure( inequivalent_indices_cations = [ indices[0] for indices in symm_struct.equivalent_indices - if valences[indices[0]] > 0.0 + if valences[indices[0]] > 0.0 # type: ignore[operator] ] se = lgf.compute_structure_environments( From 1f653296cb5ca41b4edeba96b5f806161e8fd11f Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 15:45:05 -0700 Subject: [PATCH 08/27] mypy directive for xas see comment on line 110 for explanation of attr assignment hack --- emmet-core/emmet/core/xas.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/emmet-core/emmet/core/xas.py b/emmet-core/emmet/core/xas.py index f9f0bc54f0..7a4b63cb97 100644 --- a/emmet-core/emmet/core/xas.py +++ b/emmet-core/emmet/core/xas.py @@ -152,7 +152,7 @@ def from_task_docs( try: total_spectrum = xanes.stitch(exafs, mode="XAFS") total_spectrum.absorbing_index = site - total_spectrum.task_ids = xanes.task_ids + exafs.task_ids + total_spectrum.task_ids = xanes.task_ids + exafs.task_ids # type: ignore[attr-defined] all_spectra.append(total_spectrum) except ValueError as e: warnings.warn(f"Warning during spectral merging in XASDoC: {e}") @@ -167,7 +167,7 @@ def from_task_docs( try: total_spectrum = l2.stitch(l3, mode="L23") total_spectrum.absorbing_index = site - total_spectrum.task_ids = l2.task_ids + l3.task_ids + total_spectrum.task_ids = l2.task_ids + l3.task_ids # type: ignore[attr-defined] all_spectra.append(total_spectrum) except ValueError as e: warnings.warn(f"Warning during spectral merging in XASDoC: {e}") @@ -193,12 +193,12 @@ def from_task_docs( avg_spectrum = site_weighted_spectrum( relevant_spectra, num_samples=num_samples ) - avg_spectrum.task_ids = [ + avg_spectrum.task_ids = [ # type: ignore[attr-defined] id for spectrum in relevant_spectra for id in spectrum.task_ids ] - avg_spectrum.last_updated = max( + avg_spectrum.last_updated = max( # type: ignore[attr-defined, type-var] [spectrum.last_updated for spectrum in relevant_spectra] ) averaged_spectra.append(avg_spectrum) From e3e769b9ebbb69fc8496e1810bfab76382f25dfd Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 15:57:17 -0700 Subject: [PATCH 09/27] type hint + mypy directive for piezodoc --- emmet-core/emmet/core/polar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emmet-core/emmet/core/polar.py b/emmet-core/emmet/core/polar.py index fa3263cf14..be99914828 100644 --- a/emmet-core/emmet/core/polar.py +++ b/emmet-core/emmet/core/polar.py @@ -106,7 +106,7 @@ def from_ionic_and_electronic( ): ionic_tensor = BasePiezoTensor.from_vasp_voigt(ionic) electronic_tensor = BasePiezoTensor.from_vasp_voigt(electronic) - total = ionic_tensor + electronic_tensor + total: BasePiezoTensor = ionic_tensor + electronic_tensor # type: ignore[assignment] # Symmeterize Convert to IEEE orientation total = total.convert_to_ieee(structure) From d5524ce05a8990dff42c9fa1ed9a0a4ff7373f0a Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 17:14:42 -0700 Subject: [PATCH 10/27] mypy type hinting + ignore directives for elasticity --- emmet-core/emmet/core/elasticity.py | 46 ++++++++++++++--------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/emmet-core/emmet/core/elasticity.py b/emmet-core/emmet/core/elasticity.py index 06edc63d3f..646f78f84a 100644 --- a/emmet-core/emmet/core/elasticity.py +++ b/emmet-core/emmet/core/elasticity.py @@ -493,7 +493,7 @@ def generate_derived_fitting_data( derived_2nd_pk_stresses = [] for d_strain, op_set in mapping.items(): - symmops, p_indices = zip(*op_set) + symmops, p_indices = zip(*op_set) # type: ignore[assignment] p_stresses = [stresses[i] for i in p_indices] d_stresses = [s.transform(op) for s, op in zip(p_stresses, symmops)] @@ -551,7 +551,7 @@ def symmetrize_stresses( def fit_elastic_tensor( strains: List[Strain], stresses: List[Stress], - eq_stress: Stress, + eq_stress: Stress | None, fitting_method: str = "finite_difference", order: int = 2, ) -> ElasticTensor: @@ -575,17 +575,17 @@ def fit_elastic_tensor( strains, stresses, eq_stress=eq_stress, order=order ) if order == 2: - result = ElasticTensor(result[0]) + result: ElasticTensor = ElasticTensor(result[0]) # type: ignore[no-redef] elif fitting_method == "pseudoinverse": - result = ElasticTensor.from_pseudoinverse(strains, stresses) + result: ElasticTensor = ElasticTensor.from_pseudoinverse(strains, stresses) # type: ignore[no-redef] elif fitting_method == "independent": - result = ElasticTensor.from_independent_strains( + result: ElasticTensor = ElasticTensor.from_independent_strains( # type: ignore[no-redef] strains, stresses, eq_stress=eq_stress ) else: raise ValueError(f"Unsupported elastic fitting method {fitting_method}") - return result + return result # type: ignore[return-value] def get_derived_properties( @@ -613,35 +613,35 @@ def get_derived_properties( decimals = 3 derived_prop = { "bulk_modulus": BulkModulus( - voigt=np.round(prop_dict["k_voigt"], decimals), - reuss=np.round(prop_dict["k_reuss"], decimals), - vrh=np.round(prop_dict["k_vrh"], decimals), + voigt=np.round(prop_dict["k_voigt"], decimals), # type: ignore[arg-type] + reuss=np.round(prop_dict["k_reuss"], decimals), # type: ignore[arg-type] + vrh=np.round(prop_dict["k_vrh"], decimals), # type: ignore[arg-type] ), "shear_modulus": ShearModulus( - voigt=np.round(prop_dict["g_voigt"], decimals), - reuss=np.round(prop_dict["g_reuss"], decimals), - vrh=np.round(prop_dict["g_vrh"], decimals), + voigt=np.round(prop_dict["g_voigt"], decimals), # type: ignore[arg-type] + reuss=np.round(prop_dict["g_reuss"], decimals), # type: ignore[arg-type] + vrh=np.round(prop_dict["g_vrh"], decimals), # type: ignore[arg-type] ), - "young_modulus": np.round(prop_dict["y_mod"], 0), - "homogeneous_poisson": np.round(prop_dict["homogeneous_poisson"], decimals), - "universal_anisotropy": np.round(prop_dict["universal_anisotropy"], decimals), + "young_modulus": np.round(prop_dict["y_mod"], 0), # type: ignore[arg-type] + "homogeneous_poisson": np.round(prop_dict["homogeneous_poisson"], decimals), # type: ignore[arg-type] + "universal_anisotropy": np.round(prop_dict["universal_anisotropy"], decimals), # type: ignore[arg-type] } if structure_prop_computed: derived_prop.update( { "sound_velocity": SoundVelocity( - transverse=prop_dict["trans_v"], - longitudinal=prop_dict["long_v"], - snyder_acoustic=prop_dict["snyder_ac"], - snyder_optical=prop_dict["snyder_opt"], - snyder_total=prop_dict["snyder_total"], + transverse=prop_dict["trans_v"], # type: ignore[arg-type] + longitudinal=prop_dict["long_v"], # type: ignore[arg-type] + snyder_acoustic=prop_dict["snyder_ac"], # type: ignore[arg-type] + snyder_optical=prop_dict["snyder_opt"], # type: ignore[arg-type] + snyder_total=prop_dict["snyder_total"], # type: ignore[arg-type] ), "thermal_conductivity": ThermalConductivity( - clarke=prop_dict["clarke_thermalcond"], - cahill=prop_dict["cahill_thermalcond"], + clarke=prop_dict["clarke_thermalcond"], # type: ignore[arg-type] + cahill=prop_dict["cahill_thermalcond"], # type: ignore[arg-type] ), - "debye_temperature": prop_dict["debye_temperature"], + "debye_temperature": prop_dict["debye_temperature"], # type: ignore[arg-type] } ) From e3819cb9f59a7cf0dbd47d6362ae7d5e47a58e08 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 17:20:58 -0700 Subject: [PATCH 11/27] mypy fix for stubs --- emmet-core/emmet/core/stubs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/emmet-core/emmet/core/stubs.py b/emmet-core/emmet/core/stubs.py index b302d1a588..2b542a92de 100644 --- a/emmet-core/emmet/core/stubs.py +++ b/emmet-core/emmet/core/stubs.py @@ -1,3 +1,4 @@ +# mypy: ignore-errors # isort: off """ This module stubs some pymatgen classes that implement custom behavior From 58ab43f3c63f126131c1f72ee451d4f07393fff0 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 17:21:25 -0700 Subject: [PATCH 12/27] mypy fix for molecules metal_binding --- emmet-core/emmet/core/molecules/metal_binding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emmet-core/emmet/core/molecules/metal_binding.py b/emmet-core/emmet/core/molecules/metal_binding.py index 4cd0a25cfc..95b3b81b2b 100644 --- a/emmet-core/emmet/core/molecules/metal_binding.py +++ b/emmet-core/emmet/core/molecules/metal_binding.py @@ -67,7 +67,7 @@ class MetalBindingData(BaseModel): description="The number of atoms neighboring the metal atom or ion of interest", ) - coordinating_atoms: Optional[List[Union[str, Species]]] = Field( + coordinating_atoms: Optional[List[str]] = Field( None, description="The elements/species coordinating the metal." ) From 127e398dccb8f25da9a9a6e688202bcdab02c7cc Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 17:24:00 -0700 Subject: [PATCH 13/27] mypy directives for qchem/molecule.py --- emmet-core/emmet/core/qchem/molecule.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/emmet-core/emmet/core/qchem/molecule.py b/emmet-core/emmet/core/qchem/molecule.py index c93344b156..7b8b1fb2e6 100644 --- a/emmet-core/emmet/core/qchem/molecule.py +++ b/emmet-core/emmet/core/qchem/molecule.py @@ -405,8 +405,8 @@ def from_tasks( ad = BabelMolAdaptor(molecule) openbabel.StereoFrom3D(ad.openbabel_mol) - inchi = ad.pybel_mol.write("inchi").strip() - inchikey = ad.pybel_mol.write("inchikey").strip() + inchi = ad.pybel_mol.write("inchi").strip() # type: ignore[attr-defined] + inchikey = ad.pybel_mol.write("inchikey").strip() # type: ignore[attr-defined] return cls.from_molecule( molecule=molecule, @@ -484,8 +484,8 @@ def construct_deprecated_molecule( ad = BabelMolAdaptor(molecule) openbabel.StereoFrom3D(ad.openbabel_mol) - inchi = ad.pybel_mol.write("inchi").strip() - inchikey = ad.pybel_mol.write("inchikey").strip() + inchi = ad.pybel_mol.write("inchi").strip() # type: ignore[attr-defined] + inchikey = ad.pybel_mol.write("inchikey").strip() # type: ignore[attr-defined] return cls.from_molecule( molecule=molecule, From 3cea285c11a78fd27b749ced37dd291bb6fc0492 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Fri, 19 Jul 2024 17:46:00 -0700 Subject: [PATCH 14/27] mypy fixes for electrodes.py --- emmet-core/emmet/core/electrode.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/emmet-core/emmet/core/electrode.py b/emmet-core/emmet/core/electrode.py index 3db8db395b..46982256a4 100644 --- a/emmet-core/emmet/core/electrode.py +++ b/emmet-core/emmet/core/electrode.py @@ -12,7 +12,7 @@ from pymatgen.apps.battery.insertion_battery import InsertionElectrode from pymatgen.analysis.phase_diagram import PhaseDiagram from pymatgen.core import Composition, Structure -from pymatgen.core.periodic_table import Element +from pymatgen.core.periodic_table import DummySpecies, Element, Species from pymatgen.entries.computed_entries import ComputedEntry, ComputedStructureEntry from emmet.core.mpid import MPID @@ -138,7 +138,7 @@ class EntriesCompositionSummary(BaseModel): description="Anonymous formulas for material entries across all voltage pairs.", ) - all_elements: Optional[List[Element]] = Field( + all_elements: Optional[List[Element | Species | DummySpecies]] = Field( None, description="Elements in material entries across all voltage pairs.", ) @@ -348,7 +348,7 @@ def from_entries( host_structure=stripped_host.as_dict(), framework=framework, battery_formula=battery_formula, - electrode_object=ie.as_dict(), + electrode_object=ie, elements=elements, nelements=len(elements), chemsys=chemsys, @@ -465,7 +465,7 @@ def from_composition_and_entries( entries_in_chemsys=entries, working_ion_symbol=working_ion_symbol, ) - d = cls.get_conversion_elec_doc(ce) + d = cls.get_conversion_elec_doc(ce) # type: ignore[arg-type] return cls(battery_id=battery_id, thermo_type=thermo_type, **d) @classmethod @@ -480,7 +480,7 @@ def from_composition_and_pd( ce = ConversionElectrode.from_composition_and_pd( comp=comp, pd=pd, working_ion_symbol=working_ion_symbol ) - d = cls.get_conversion_elec_doc(ce) + d = cls.get_conversion_elec_doc(ce) # type: ignore[arg-type] return cls(battery_id=battery_id, thermo_type=thermo_type, **d) @staticmethod From 3b54fed362f1f00329ba630a72481903e12a26ee Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 09:03:53 -0700 Subject: [PATCH 15/27] mypy fixes for electronic_structure --- emmet-core/emmet/core/electronic_structure.py | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/emmet-core/emmet/core/electronic_structure.py b/emmet-core/emmet/core/electronic_structure.py index 54b38c2144..50e5873f38 100644 --- a/emmet-core/emmet/core/electronic_structure.py +++ b/emmet-core/emmet/core/electronic_structure.py @@ -1,10 +1,12 @@ """ Core definition of an Electronic Structure """ + from __future__ import annotations from collections import defaultdict from datetime import datetime +from enum import Enum from math import isnan -from typing import Dict, Optional, Type, TypeVar, Union, List +from typing import Dict, List, Optional, Type, TypeVar, Union import numpy as np from pydantic import BaseModel, Field @@ -20,11 +22,10 @@ from pymatgen.symmetry.analyzer import SpacegroupAnalyzer from pymatgen.symmetry.bandstructure import HighSymmKpath from typing_extensions import Literal -from enum import Enum -from emmet.core.settings import EmmetSettings from emmet.core.material_property import PropertyDoc from emmet.core.mpid import MPID +from emmet.core.settings import EmmetSettings SETTINGS = EmmetSettings() @@ -279,7 +280,7 @@ def from_bsdos( # type: ignore[override] except KeyError: spin_polarization = None - dos_data["total"][spin] = DosSummaryData( + dos_data["total"][spin] = DosSummaryData( # type: ignore[index] task_id=dos_task, band_gap=band_gap, cbm=cbm, @@ -296,7 +297,7 @@ def from_bsdos( # type: ignore[override] spin_polarization = None - dos_data["orbital"][orbital][spin] = DosSummaryData( + dos_data["orbital"][orbital][spin] = DosSummaryData( # type: ignore[index] task_id=dos_task, band_gap=band_gap, cbm=cbm, @@ -309,7 +310,7 @@ def from_bsdos( # type: ignore[override] for ele in ele_dos: orb_dos = dos_obj.get_element_spd_dos(ele) - for orbital in ["total"] + list(orb_dos.keys()): + for orbital in ["total"] + list(orb_dos.keys()): # type: ignore[assignment] if orbital == "total": proj_dos = ele_dos label = ele @@ -323,7 +324,7 @@ def from_bsdos( # type: ignore[override] spin_polarization = None - dos_data["elemental"][ele][orbital][spin] = DosSummaryData( + dos_data["elemental"][ele][orbital][spin] = DosSummaryData( # type: ignore[index] task_id=dos_task, band_gap=band_gap, cbm=cbm, @@ -349,7 +350,7 @@ def from_bsdos( # type: ignore[override] ).ordering else: bs_mag_ordering = CollinearMagneticStructureAnalyzer( - bs.structure + bs.structure # type: ignore[arg-type] ).ordering gap_dict = bs.get_band_gap() @@ -358,8 +359,8 @@ def from_bsdos( # type: ignore[override] if is_metal: band_gap = 0.0 - cbm = None - vbm = None + cbm = None # type: ignore[assignment] + vbm = None # type: ignore[assignment] is_gap_direct = False else: band_gap = gap_dict["energy"] @@ -399,7 +400,7 @@ def from_bsdos( # type: ignore[override] if not gen_labels.issubset(kpath_labels): new_structure = SpacegroupAnalyzer( - bs.structure + bs.structure # type: ignore[arg-type] ).get_primitive_standard_structure( international_monoclinic=False ) @@ -428,7 +429,7 @@ def from_bsdos( # type: ignore[override] ) bs_entry = BandstructureData(**bs_data) # type: ignore - dos_entry = DosData(**dos_data) + dos_entry = DosData(**dos_data) # type: ignore[arg-type] # Obtain summary data From a6ec3f55042a5a3f20787564573e2e1890e04db9 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 09:47:53 -0700 Subject: [PATCH 16/27] pre-commit formatting --- emmet-core/emmet/core/thermo.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/emmet-core/emmet/core/thermo.py b/emmet-core/emmet/core/thermo.py index f3cef9fbd3..c687d00366 100644 --- a/emmet-core/emmet/core/thermo.py +++ b/emmet-core/emmet/core/thermo.py @@ -201,9 +201,11 @@ def _energy_eval(entry: Union[ComputedStructureEntry, ComputedEntry]): # Store different info if stable vs decomposes if d["is_stable"]: - d["equilibrium_reaction_energy_per_atom"] = ( - pd.get_equilibrium_reaction_energy(blessed_entry) # type: ignore[arg-type] - ) + d[ + "equilibrium_reaction_energy_per_atom" + ] = pd.get_equilibrium_reaction_energy( + blessed_entry + ) # type: ignore[arg-type] else: d["decomposes_to"] = [ { From 22cfe4ea4b7caf9b306acdc71b4d46f17e68a5a7 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 10:18:09 -0700 Subject: [PATCH 17/27] future annotations for type hint compatibility w/ py3.9 --- emmet-core/emmet/core/feff/task.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/emmet-core/emmet/core/feff/task.py b/emmet-core/emmet/core/feff/task.py index ab94ae5b6d..74c50d8d1a 100644 --- a/emmet-core/emmet/core/feff/task.py +++ b/emmet-core/emmet/core/feff/task.py @@ -1,4 +1,7 @@ """ Core definition of a VASP Task Document """ + +from __future__ import annotations + from typing import Any, Dict, List from pydantic import Field @@ -7,8 +10,8 @@ from pymatgen.core.periodic_table import Element, Species from emmet.core.structure import StructureMetadata -from emmet.core.vasp.task_valid import TaskDocument as BaseTaskDocument from emmet.core.utils import ValueEnum +from emmet.core.vasp.task_valid import TaskDocument as BaseTaskDocument class CalcType(ValueEnum): From ad2213079453a1850bc9e2301bae586b9a57d5b2 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 10:32:06 -0700 Subject: [PATCH 18/27] more py3.9 typing compat fixes --- emmet-core/emmet/core/elasticity.py | 2 ++ emmet-core/emmet/core/electrode.py | 17 +++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/emmet-core/emmet/core/elasticity.py b/emmet-core/emmet/core/elasticity.py index 646f78f84a..4ac8ed1857 100644 --- a/emmet-core/emmet/core/elasticity.py +++ b/emmet-core/emmet/core/elasticity.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import Any, Dict, List, Optional, Tuple, Union import numpy as np diff --git a/emmet-core/emmet/core/electrode.py b/emmet-core/emmet/core/electrode.py index 46982256a4..8e98a9b98c 100644 --- a/emmet-core/emmet/core/electrode.py +++ b/emmet-core/emmet/core/electrode.py @@ -1,21 +1,22 @@ +from __future__ import annotations + import re -from datetime import datetime -from typing import List, Union, Dict, Optional from collections import defaultdict +from datetime import datetime +from typing import Dict, List, Optional, Union -from emmet.core.utils import ValueEnum -from emmet.core.common import convert_datetime - -from pydantic import field_validator, BaseModel, Field +from pydantic import BaseModel, Field, field_validator +from pymatgen.analysis.phase_diagram import PhaseDiagram from pymatgen.apps.battery.battery_abc import AbstractElectrode from pymatgen.apps.battery.conversion_battery import ConversionElectrode from pymatgen.apps.battery.insertion_battery import InsertionElectrode -from pymatgen.analysis.phase_diagram import PhaseDiagram from pymatgen.core import Composition, Structure from pymatgen.core.periodic_table import DummySpecies, Element, Species from pymatgen.entries.computed_entries import ComputedEntry, ComputedStructureEntry +from emmet.core.common import convert_datetime from emmet.core.mpid import MPID +from emmet.core.utils import ValueEnum class BatteryType(str, ValueEnum): @@ -138,7 +139,7 @@ class EntriesCompositionSummary(BaseModel): description="Anonymous formulas for material entries across all voltage pairs.", ) - all_elements: Optional[List[Element | Species | DummySpecies]] = Field( + all_elements: Optional[List[Union[Element, Species, DummySpecies]]] = Field( None, description="Elements in material entries across all voltage pairs.", ) From b1b5f42333ffef465869fabb2970de9effa1bf58 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 10:50:57 -0700 Subject: [PATCH 19/27] mypy directive on wrong line --- emmet-core/emmet/core/thermo.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/emmet-core/emmet/core/thermo.py b/emmet-core/emmet/core/thermo.py index c687d00366..2a47a5678d 100644 --- a/emmet-core/emmet/core/thermo.py +++ b/emmet-core/emmet/core/thermo.py @@ -1,17 +1,18 @@ """ Core definition of a Thermo Document """ + from collections import defaultdict -from typing import Dict, List, Optional, Union from datetime import datetime -from emmet.core.base import EmmetMeta -from emmet.core.utils import ValueEnum +from typing import Dict, List, Optional, Union from pydantic import BaseModel, Field from pymatgen.analysis.phase_diagram import PhaseDiagram from pymatgen.entries.computed_entries import ComputedEntry, ComputedStructureEntry -from emmet.core.material_property import PropertyDoc +from emmet.core.base import EmmetMeta from emmet.core.material import PropertyOrigin +from emmet.core.material_property import PropertyDoc from emmet.core.mpid import MPID +from emmet.core.utils import ValueEnum from emmet.core.vasp.calc_types.enums import RunType @@ -204,8 +205,8 @@ def _energy_eval(entry: Union[ComputedStructureEntry, ComputedEntry]): d[ "equilibrium_reaction_energy_per_atom" ] = pd.get_equilibrium_reaction_energy( - blessed_entry - ) # type: ignore[arg-type] + blessed_entry # type: ignore[arg-type] + ) else: d["decomposes_to"] = [ { From 8fe8e9087a4580da4b5b3a6df9f536702d664b30 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:14:04 -0700 Subject: [PATCH 20/27] builers - provenance mypy fix --- emmet-builders/emmet/builders/materials/provenance.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/emmet-builders/emmet/builders/materials/provenance.py b/emmet-builders/emmet/builders/materials/provenance.py index d2525b64aa..6a88e1e019 100644 --- a/emmet-builders/emmet/builders/materials/provenance.py +++ b/emmet-builders/emmet/builders/materials/provenance.py @@ -1,7 +1,7 @@ from collections import defaultdict -from typing import Dict, Iterable, List, Optional, Tuple -from math import ceil from datetime import datetime +from math import ceil +from typing import Dict, Iterable, List, Optional, Tuple from maggma.core import Builder, Store from maggma.utils import grouper @@ -168,7 +168,7 @@ def get_items(self) -> Tuple[List[Dict], List[Dict]]: # type: ignore for snl in snls: struc = Structure.from_dict(snl) snl_sg = get_sg(struc) - struc.snl = SNLDict(**snl) + struc.snl = SNLDict(**snl) # type: ignore[attr-defined] snl_groups[snl_sg].append(struc) mat_sg = get_sg(Structure.from_dict(mat["structure"])) From b0b4c8b23063ab7e2b264406546136dfbb99d7cd Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:17:04 -0700 Subject: [PATCH 21/27] builders - mypy for ce builder --- .../builders/materials/corrected_entries.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/emmet-builders/emmet/builders/materials/corrected_entries.py b/emmet-builders/emmet/builders/materials/corrected_entries.py index 0c819217fe..cf4632281c 100644 --- a/emmet-builders/emmet/builders/materials/corrected_entries.py +++ b/emmet-builders/emmet/builders/materials/corrected_entries.py @@ -1,20 +1,20 @@ +import copy import warnings from collections import defaultdict +from datetime import datetime from itertools import chain -from typing import Dict, Iterable, Iterator, List, Optional from math import ceil -import copy -from datetime import datetime +from typing import Dict, Iterable, Iterator, List, Optional, Union from maggma.core import Builder, Store from maggma.utils import grouper -from pymatgen.entries.computed_entries import ComputedStructureEntry from pymatgen.entries.compatibility import Compatibility +from pymatgen.entries.computed_entries import ComputedStructureEntry -from emmet.core.utils import jsanitize -from emmet.builders.utils import chemsys_permutations, HiddenPrints -from emmet.core.thermo import ThermoType +from emmet.builders.utils import HiddenPrints, chemsys_permutations from emmet.core.corrected_entries import CorrectedEntriesDoc +from emmet.core.thermo import ThermoType +from emmet.core.utils import jsanitize class CorrectedEntriesBuilder(Builder): @@ -24,7 +24,7 @@ def __init__( corrected_entries: Store, oxidation_states: Optional[Store] = None, query: Optional[Dict] = None, - compatibility: Optional[List[Compatibility]] = None, + compatibility: Optional[Union[List[Compatibility], List[None]]] = [None], chunk_size: int = 1000, **kwargs, ): @@ -45,10 +45,10 @@ def __init__( self.materials = materials self.query = query if query else {} self.corrected_entries = corrected_entries - self.compatibility = compatibility or [None] + self.compatibility = compatibility self.oxidation_states = oxidation_states self.chunk_size = chunk_size - self._entries_cache: Dict[str, List[ComputedStructureEntry]] = defaultdict(list) + self._entries_cache: Dict[str, List[Dict]] = defaultdict(list) if self.corrected_entries.key != "chemsys": warnings.warn( From 093ed667958d9090eb0e16b31c027ef8525bdf2d Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:17:22 -0700 Subject: [PATCH 22/27] builders - mypy for utils --- emmet-builders/emmet/builders/utils.py | 28 ++++++++++++++------------ 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/emmet-builders/emmet/builders/utils.py b/emmet-builders/emmet/builders/utils.py index 75633637ef..42948a9037 100644 --- a/emmet-builders/emmet/builders/utils.py +++ b/emmet-builders/emmet/builders/utils.py @@ -1,17 +1,19 @@ from __future__ import annotations -from typing import Set, Union, Any, Literal, Optional -import sys + +import json import os -from pathlib import Path +import sys from gzip import GzipFile -import orjson -import json from io import BytesIO -from monty.serialization import MontyDecoder -from botocore.exceptions import ClientError from itertools import chain, combinations -from pymatgen.core import Structure +from pathlib import Path +from typing import Any, Literal, Optional, Set, Union + +import orjson +from botocore.exceptions import ClientError +from monty.serialization import MontyDecoder from pymatgen.analysis.diffusion.neb.full_path_mapper import MigrationGraph +from pymatgen.core import Structure from pymatgen.io.vasp.inputs import PotcarSingle from emmet.builders.settings import EmmetBuildSettings @@ -275,16 +277,16 @@ def get_potcar_stats( # fallback method for validation - use header hash and symbol # note that the potcar_spec assigns PotcarSingle.symbol to "titel" # whereas the ***correct*** field is `header` - summary_stats["titel"] = potcar.header - summary_stats["hash"] = potcar.md5_header_hash - summary_stats = [summary_stats] + summary_stats["titel"] = potcar.header # type: ignore[assignment] + summary_stats["hash"] = potcar.md5_header_hash # type: ignore[assignment] + summary_stats = [summary_stats] # type: ignore[assignment] elif method == "pymatgen": - summary_stats = [] + summary_stats = [] # type: ignore[assignment] for _, entries in PotcarSingle._potcar_summary_stats[ functional ].items(): - summary_stats += [ + summary_stats += [ # type: ignore[operator] {**entry, "titel": None, "hash": None} for entry in entries if entry["symbol"] == potcar_symbol From 3677afd7eb34e762e3a46d9f8f54d78df91991c6 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:27:42 -0700 Subject: [PATCH 23/27] builders - mypy for phonon --- .../emmet/builders/abinit/phonon.py | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/emmet-builders/emmet/builders/abinit/phonon.py b/emmet-builders/emmet/builders/abinit/phonon.py index 9be05d7b91..8974b5bd29 100644 --- a/emmet-builders/emmet/builders/abinit/phonon.py +++ b/emmet-builders/emmet/builders/abinit/phonon.py @@ -1,41 +1,39 @@ -import tempfile import os +import tempfile from math import ceil -from emmet.builders.settings import EmmetBuildSettings +from typing import Dict, Iterator, List, Optional, Tuple + import numpy as np -from typing import Optional, Dict, List, Iterator, Tuple +from abipy.abio.inputs import AnaddbInput +from abipy.core.abinit_units import eV_to_THz +from abipy.dfpt.anaddbnc import AnaddbNcFile +from abipy.dfpt.ddb import AnaddbError, DdbFile, DielectricTensorGenerator +from abipy.dfpt.phonons import PhononBands +from abipy.flowtk.tasks import AnaddbTask, TaskManager +from maggma.builders import Builder +from maggma.core import Store from maggma.utils import grouper - +from pymatgen.core.structure import Structure +from pymatgen.io.abinit.abiobjects import KSampling from pymatgen.phonon.bandstructure import PhononBandStructureSymmLine from pymatgen.phonon.dos import CompletePhononDos from pymatgen.phonon.ir_spectra import IRDielectricTensor -from pymatgen.core.structure import Structure -from pymatgen.io.abinit.abiobjects import KSampling -from pymatgen.symmetry.bandstructure import HighSymmKpath from pymatgen.symmetry.analyzer import SpacegroupAnalyzer -from abipy.dfpt.anaddbnc import AnaddbNcFile -from abipy.abio.inputs import AnaddbInput -from abipy.flowtk.tasks import AnaddbTask, TaskManager -from abipy.dfpt.ddb import AnaddbError, DielectricTensorGenerator, DdbFile -from abipy.dfpt.phonons import PhononBands -from abipy.core.abinit_units import eV_to_THz -from maggma.builders import Builder -from maggma.core import Store +from pymatgen.symmetry.bandstructure import HighSymmKpath +from emmet.builders.settings import EmmetBuildSettings from emmet.core.phonon import ( - PhononWarnings, - ThermodynamicProperties, AbinitPhonon, - VibrationalEnergy, -) -from emmet.core.phonon import ( - PhononDos, + Ddb, PhononBandStructure, + PhononDos, + PhononWarnings, PhononWebsiteBS, - Ddb, ThermalDisplacement, + ThermodynamicProperties, + VibrationalEnergy, ) -from emmet.core.polar import DielectricDoc, BornEffectiveCharges, IRDielectric +from emmet.core.polar import BornEffectiveCharges, DielectricDoc, IRDielectric from emmet.core.utils import jsanitize SETTINGS = EmmetBuildSettings() @@ -679,7 +677,7 @@ def get_pmg_bs( ) ph_bs_sl = PhononBandStructureSymmLine( - qpoints=qpts, + qpoints=qpts, # type: ignore[arg-type] frequencies=ph_freqs, lattice=structure.reciprocal_lattice, has_nac=phbands.non_anal_ph is not None, From c87b93111ad3e5dd683766e007c07f11c7059198 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:28:00 -0700 Subject: [PATCH 24/27] builders - mypy for alloys --- .../emmet/builders/materials/alloys.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/emmet-builders/emmet/builders/materials/alloys.py b/emmet-builders/emmet/builders/materials/alloys.py index 35831db5b1..04276c9b92 100644 --- a/emmet-builders/emmet/builders/materials/alloys.py +++ b/emmet-builders/emmet/builders/materials/alloys.py @@ -1,19 +1,19 @@ -from itertools import combinations, chain -from typing import Tuple, List, Dict, Union +from itertools import chain, combinations +from typing import Dict, List, Tuple, Union -from tqdm import tqdm from maggma.builders import Builder -from pymatgen.core.structure import Structure from matminer.datasets import load_dataset -from emmet.core.thermo import ThermoType - from pymatgen.analysis.alloys.core import ( - AlloyPair, - InvalidAlloy, KNOWN_ANON_FORMULAS, AlloyMember, + AlloyPair, AlloySystem, + InvalidAlloy, ) +from pymatgen.core.structure import Structure +from tqdm import tqdm + +from emmet.core.thermo import ThermoType # rough sort of ANON_FORMULAS by "complexity" ANON_FORMULAS = sorted(KNOWN_ANON_FORMULAS, key=lambda af: len(af)) @@ -291,7 +291,7 @@ def process_item(self, item: Tuple[List[AlloyPair], Dict[str, Structure]]): is_ordered=structure.is_ordered, x=pair.get_x(structure.composition), ) - pair_members["members"].append(member.as_dict()) + pair_members["members"].append(member.as_dict()) # type: ignore[attr-defined] except Exception as exc: print(f"Exception for {db_id}: {exc}") if pair_members["members"]: From 117989f277e949692de4e659daf9bdc6f364a44b Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:29:01 -0700 Subject: [PATCH 25/27] builders - mypy for electrodes --- .../emmet/builders/materials/electrodes.py | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/emmet-builders/emmet/builders/materials/electrodes.py b/emmet-builders/emmet/builders/materials/electrodes.py index b00114ec63..a93e840426 100644 --- a/emmet-builders/emmet/builders/materials/electrodes.py +++ b/emmet-builders/emmet/builders/materials/electrodes.py @@ -1,22 +1,22 @@ import operator +from collections import defaultdict from datetime import datetime from functools import lru_cache from itertools import chain from math import ceil -from typing import Any, Iterator, Dict, List, Optional -from collections import defaultdict +from typing import Any, Dict, Iterator, List, Optional from maggma.builders import Builder from maggma.stores import MongoStore from maggma.utils import grouper -from pymatgen.entries.computed_entries import ComputedEntry, ComputedStructureEntry +from pymatgen.analysis.phase_diagram import Composition, PhaseDiagram from pymatgen.entries.compatibility import MaterialsProject2020Compatibility -from pymatgen.analysis.phase_diagram import PhaseDiagram, Composition +from pymatgen.entries.computed_entries import ComputedEntry, ComputedStructureEntry -from emmet.core.electrode import InsertionElectrodeDoc, ConversionElectrodeDoc +from emmet.builders.settings import EmmetBuildSettings +from emmet.core.electrode import ConversionElectrodeDoc, InsertionElectrodeDoc from emmet.core.structure_group import StructureGroupDoc, _get_id_lexi from emmet.core.utils import jsanitize -from emmet.builders.settings import EmmetBuildSettings def s_hash(el): @@ -115,9 +115,9 @@ def __init__( self.check_newer = check_newer self.chunk_size = chunk_size - self.query[ - "deprecated" - ] = False # Ensure only non-deprecated materials are chosen + self.query["deprecated"] = ( + False # Ensure only non-deprecated materials are chosen + ) super().__init__( sources=[materials], targets=[sgroups], chunk_size=chunk_size, **kwargs @@ -465,9 +465,9 @@ def update_targets(self, items: List): if len(items) > 0: self.logger.info("Updating {} battery documents".format(len(items))) for struct_group_dict in items: - struct_group_dict[ - self.grouped_materials.last_updated_field - ] = datetime.utcnow() + struct_group_dict[self.grouped_materials.last_updated_field] = ( + datetime.utcnow() + ) self.insertion_electrode.update(docs=items, key=["battery_id"]) else: self.logger.info("No items to update") @@ -564,9 +564,11 @@ def process_item(self, item) -> Dict: # Get lowest material_id with matching composition material_ids = [ ( - lambda x: x.data["material_id"] - if x.composition.reduced_formula == v[1].reduced_formula - else None + lambda x: ( + x.data["material_id"] # type: ignore[attr-defined] + if x.composition.reduced_formula == v[1].reduced_formula + else None + ) )(e) for e in pd.entries ] @@ -597,7 +599,7 @@ def process_item(self, item) -> Dict: relevant_entry_data = [] for e in pd.entries: if e.composition == Composition(c): - relevant_entry_data.append((e.energy_per_atom, e.entry_id)) + relevant_entry_data.append((e.energy_per_atom, e.entry_id)) # type: ignore[attr-defined] relevant_entry_data.sort(key=lambda x: x[0]) entry_id_mapping[c] = relevant_entry_data[0][1] From b77e622de85a4bde8101869824e24e9389a7b4e6 Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:57:03 -0700 Subject: [PATCH 26/27] API - mypy fix for FindStructureQuery operator also remove skip for FindStructureQuery test --- .../materials/materials/query_operators.py | 17 +++++++++-------- .../materials/materials/test_query_operators.py | 9 ++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/emmet-api/emmet/api/routes/materials/materials/query_operators.py b/emmet-api/emmet/api/routes/materials/materials/query_operators.py index 7dff0439a0..122b6570a2 100644 --- a/emmet-api/emmet/api/routes/materials/materials/query_operators.py +++ b/emmet-api/emmet/api/routes/materials/materials/query_operators.py @@ -1,18 +1,19 @@ from itertools import permutations -from typing import Literal, Optional +from typing import Dict, Literal, Optional -from emmet.core.symmetry import CrystalSystem from fastapi import Body, HTTPException, Query from maggma.api.query_operator import QueryOperator from maggma.api.utils import STORE_PARAMS -from emmet.api.routes.materials.materials.utils import ( - formula_to_criteria, - chemsys_to_criteria, -) from pymatgen.analysis.structure_matcher import ElementComparator, StructureMatcher from pymatgen.core.composition import Composition, CompositionError from pymatgen.core.periodic_table import Element from pymatgen.core.structure import Structure + +from emmet.api.routes.materials.materials.utils import ( + chemsys_to_criteria, + formula_to_criteria, +) +from emmet.core.symmetry import CrystalSystem from emmet.core.vasp.calc_types import RunType @@ -281,9 +282,9 @@ class FindStructureQuery(QueryOperator): def query( self, - structure: Structure = Body( + structure: Dict = Body( ..., - description="Pymatgen structure object to query with", + description="Dictionary representaion of Pymatgen structure object to query with", ), ltol: float = Query( 0.2, diff --git a/emmet-api/tests/materials/materials/test_query_operators.py b/emmet-api/tests/materials/materials/test_query_operators.py index e4266ee3ea..8033bdebb5 100644 --- a/emmet-api/tests/materials/materials/test_query_operators.py +++ b/emmet-api/tests/materials/materials/test_query_operators.py @@ -1,6 +1,9 @@ import os -import pytest +from monty.serialization import dumpfn, loadfn +from monty.tempfile import ScratchDir +from pymatgen.core.structure import Structure + from emmet.api.core.settings import MAPISettings from emmet.api.routes.materials.materials.query_operators import ( ChemsysQuery, @@ -14,9 +17,6 @@ SymmetryQuery, ) from emmet.core.symmetry import CrystalSystem -from monty.serialization import dumpfn, loadfn -from monty.tempfile import ScratchDir -from pymatgen.core.structure import Structure def test_formula_query(): @@ -158,7 +158,6 @@ def test_multi_material_id_query(): } -@pytest.mark.skip(reason="Pmg cif reader issue") def test_find_structure_query(): op = FindStructureQuery() From 6f985f66b40d91f444fbb2ba46b44eb51ddb4efc Mon Sep 17 00:00:00 2001 From: Tyler Mathis <35553152+tsmathis@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:59:49 -0700 Subject: [PATCH 27/27] pre-commit linting --- .../emmet/builders/materials/electrodes.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/emmet-builders/emmet/builders/materials/electrodes.py b/emmet-builders/emmet/builders/materials/electrodes.py index a93e840426..a7b2c23f2c 100644 --- a/emmet-builders/emmet/builders/materials/electrodes.py +++ b/emmet-builders/emmet/builders/materials/electrodes.py @@ -115,9 +115,9 @@ def __init__( self.check_newer = check_newer self.chunk_size = chunk_size - self.query["deprecated"] = ( - False # Ensure only non-deprecated materials are chosen - ) + self.query[ + "deprecated" + ] = False # Ensure only non-deprecated materials are chosen super().__init__( sources=[materials], targets=[sgroups], chunk_size=chunk_size, **kwargs @@ -465,9 +465,9 @@ def update_targets(self, items: List): if len(items) > 0: self.logger.info("Updating {} battery documents".format(len(items))) for struct_group_dict in items: - struct_group_dict[self.grouped_materials.last_updated_field] = ( - datetime.utcnow() - ) + struct_group_dict[ + self.grouped_materials.last_updated_field + ] = datetime.utcnow() self.insertion_electrode.update(docs=items, key=["battery_id"]) else: self.logger.info("No items to update")