Skip to content

Commit b28c937

Browse files
authored
Ion: fix CO2- and I3- parsing errors; enhance tests (materialsproject#3991)
1 parent 1bfd330 commit b28c937

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/pymatgen/core/ion.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def get_reduced_formula_and_factor(
226226
elif formula == "CSN":
227227
formula = "SCN"
228228
# triiodide, nitride, an phosphide
229-
elif formula in ["I", "N", "P"] and self.charge == -1:
229+
elif (formula in ["N", "P"] and self.charge == -1) or (formula == "I" and self.charge == 1 / 3):
230230
formula += "3"
231231
factor /= 3
232232
# formate # codespell:ignore
@@ -235,7 +235,7 @@ def get_reduced_formula_and_factor(
235235
# oxalate
236236
elif formula == "CO2":
237237
formula = "C2O4"
238-
factor *= 2
238+
factor /= 2
239239
# diatomic gases
240240
elif formula in {"O", "N", "F", "Cl", "H"} and factor % 2 == 0:
241241
formula += "2"

tests/core/test_ion.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def test_special_formulas(self):
5656
("Cl-", "Cl[-1]"),
5757
("H+", "H[+1]"),
5858
("F-", "F[-1]"),
59+
("I-", "I[-1]"),
5960
("F2", "F2(aq)"),
6061
("H2", "H2(aq)"),
6162
("O3", "O3(aq)"),
@@ -69,6 +70,7 @@ def test_special_formulas(self):
6970
("CH3COOH", "CH3COOH(aq)"),
7071
("CH3OH", "CH3OH(aq)"),
7172
("H4CO", "CH3OH(aq)"),
73+
("CO2-", "C2O4[-2]"),
7274
("CH4", "CH4(aq)"),
7375
("NH4+", "NH4[+1]"),
7476
("NH3", "NH3(aq)"),
@@ -81,7 +83,9 @@ def test_special_formulas(self):
8183
("Zr(OH)4", "Zr(OH)4(aq)"),
8284
]
8385
for tup in special_formulas:
84-
assert Ion.from_formula(tup[0]).reduced_formula == tup[1]
86+
assert (
87+
Ion.from_formula(tup[0]).reduced_formula == tup[1]
88+
), f"Expected {tup[1]} but got {Ion.from_formula(tup[0]).reduced_formula}"
8589

8690
assert Ion.from_formula("Fe(OH)4+").get_reduced_formula_and_factor(hydrates=True) == ("FeO2.2H2O", 1)
8791
assert Ion.from_formula("Zr(OH)4").get_reduced_formula_and_factor(hydrates=True) == ("ZrO2.2H2O", 1)

0 commit comments

Comments
 (0)