From 39a761d2bc1e97a95fac9da05908ba65037959b8 Mon Sep 17 00:00:00 2001 From: "Haoyu (Daniel)" Date: Sat, 25 Jan 2025 17:23:34 +0100 Subject: [PATCH] add test for composition error --- src/pymatgen/core/composition.py | 6 +++--- tests/core/test_composition.py | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/pymatgen/core/composition.py b/src/pymatgen/core/composition.py index 7fb85cb6f14..e400a42c9b0 100644 --- a/src/pymatgen/core/composition.py +++ b/src/pymatgen/core/composition.py @@ -1327,7 +1327,7 @@ def reduce_formula( sym_amt: Mapping[str, float], iupac_ordering: bool = False, ) -> tuple[str, int]: - """Helper function to reduce `sym_amt` to a reduced formula and factor. + """Helper function to reduce a symbol-amount mapping. Args: sym_amt (dict[str, float]): Symbol to amount mapping. @@ -1352,10 +1352,10 @@ def reduce_formula( if all(int(i) == i for i in sym_amt.values()): factor = abs(gcd(*(int(i) for i in sym_amt.values()))) - poly_anions: list[str] = [] # If the composition contains polyanion + poly_anions: list[str] = [] if len(syms) >= 3 and get_el_sp(syms[-1]).X - get_el_sp(syms[-2]).X < 1.65: - poly_sym_amt = {syms[i]: sym_amt[syms[i]] / factor for i in [-2, -1]} + poly_sym_amt: dict[str, float] = {syms[i]: sym_amt[syms[i]] / factor for i in (-2, -1)} poly_form, poly_factor = reduce_formula(poly_sym_amt, iupac_ordering=iupac_ordering) if poly_factor != 1: diff --git a/tests/core/test_composition.py b/tests/core/test_composition.py index a489b07f25d..00366b253d4 100644 --- a/tests/core/test_composition.py +++ b/tests/core/test_composition.py @@ -12,7 +12,7 @@ from pytest import approx from pymatgen.core import Composition, DummySpecies, Element, Species -from pymatgen.core.composition import ChemicalPotential +from pymatgen.core.composition import ChemicalPotential, CompositionError from pymatgen.util.testing import PymatgenTest @@ -860,6 +860,12 @@ def test_isotopes(self): assert "Deuterium" in [elem.long_name for elem in composition.elements] +def test_composition_error(): + error = CompositionError("Composition error") + assert isinstance(error, CompositionError) + assert str(error) == "Composition error" + + class TestChemicalPotential: def test_init(self): dct = {"Fe": 1, Element("Fe"): 1}