Skip to content

Commit

Permalink
revert Composition int support and test from int/float raises
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Mar 20, 2024
1 parent 7823bd1 commit a630aed
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
5 changes: 1 addition & 4 deletions pymatgen/core/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,9 @@ def __init__(self, *args, strict: bool = False, **kwargs) -> None:
"""
# allow_negative must be popped from **kwargs due to *args ambiguity
self.allow_negative = kwargs.pop("allow_negative", False)
if len(args) == 1 and isinstance(args[0], (int, float)):
# treat single integer/float as atomic number
elem_map = {get_el_sp(args[0]): 1}
# it's much faster to recognize a composition and use the el_map than
# to pass the composition to {}
elif len(args) == 1 and isinstance(args[0], Composition):
if len(args) == 1 and isinstance(args[0], Composition):
elem_map = args[0]
elif len(args) == 1 and isinstance(args[0], str):
elem_map = self._parse_formula(args[0]) # type: ignore[assignment]
Expand Down
18 changes: 5 additions & 13 deletions tests/core/test_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ def test_init(self):
comp = Composition({"S": Composition.amount_tolerance / 2})
assert len(comp.elements) == 0

# test from int/float
assert Composition(1) == Composition("H")
assert Composition(2.0) == Composition("He")
# test Composition from int/float raises
for val in (1, 2.5):
with pytest.raises(ValueError, match=f"Can't parse Element or Species from {val}"):
Composition(val)

def test_str_and_repr(self):
test_cases = [
Expand All @@ -164,16 +165,7 @@ def test_str_and_repr(self):
assert repr(Composition(comp)) == expected["repr"]

def test_average_electroneg(self):
electro_negs = (
2.7224999999999997,
2.4160000000000004,
2.5485714285714285,
2.21,
2.718,
3.08,
1.21,
2.43,
)
electro_negs = (2.722499, 2.416000, 2.548571, 2.21, 2.718, 3.08, 1.21, 2.43)
for elem, val in zip(self.comps, electro_negs):
assert elem.average_electroneg == approx(val)

Expand Down

0 comments on commit a630aed

Please sign in to comment.