Skip to content

Commit fa6309c

Browse files
committed
test add_site_property() ValueError msg
1 parent b0e4eb2 commit fa6309c

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

pymatgen/core/structure.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,12 @@ def add_site_property(self, property_name: str, values: Sequence | np.ndarray) -
489489
property_name (str): The name of the property to add.
490490
values (list): A sequence of values. Must be same length as
491491
number of sites.
492+
493+
Raises:
494+
ValueError: if len(values) != number of sites.
492495
"""
493496
if len(values) != len(self):
494-
raise ValueError(f"Values has length {len(values)} but there are {len(self)} sites! Must be same length.")
497+
raise ValueError(f"{len(values)=} must equal sites in structure={len(self)}")
495498
for site, val in zip(self, values):
496499
site.properties[property_name] = val
497500

tests/core/test_structure.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,11 @@ def test_add_site_property(self):
21482148
assert self.mol[0].charge == 4.1
21492149
assert self.mol[0].magmom == 3
21502150
self.mol.remove_site_property("magmom")
2151+
2152+
# test ValueError when values have wrong length
2153+
with pytest.raises(ValueError, match="len(values)=2 must equal sites in structure=5"):
2154+
self.mol.add_site_property("charge", [4, 2])
2155+
21512156
with pytest.raises(AttributeError, match="attr='magmom' not found on Site"):
21522157
_ = self.mol[0].magmom
21532158

0 commit comments

Comments
 (0)