From 4b10297f3ebb65c251b91f803121d967437222c3 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Thu, 19 Feb 2026 13:41:55 -0600 Subject: [PATCH] fix missing raise --- src/probeinterface/probe.py | 7 ++++--- tests/test_probegroup.py | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/probeinterface/probe.py b/src/probeinterface/probe.py index 76c64c6..4867d35 100644 --- a/src/probeinterface/probe.py +++ b/src/probeinterface/probe.py @@ -570,14 +570,15 @@ def set_contact_ids(self, contact_ids: np.array | list): self._contact_ids = None return - assert np.unique(contact_ids).size == contact_ids.size, "Contact ids have to be unique within a Probe" - if contact_ids.size != self.get_contact_count(): - ValueError( + raise ValueError( f"contact_ids {contact_ids.size} do not have the same size " f"as number of contacts {self.get_contact_count()}" ) + if np.unique(contact_ids).size != contact_ids.size: + raise ValueError("contact_ids must be unique within a Probe") + if contact_ids.dtype.kind != "U": contact_ids = contact_ids.astype("U") diff --git a/tests/test_probegroup.py b/tests/test_probegroup.py index 34a01ec..56bf97d 100644 --- a/tests/test_probegroup.py +++ b/tests/test_probegroup.py @@ -92,6 +92,30 @@ def test_probegroup_allows_duplicate_positions_across_probes(): assert len(group.probes) == 2 +def test_set_contact_ids_rejects_within_probe_duplicates(): + """Setting duplicate contact_ids within a single probe raises ValueError.""" + from probeinterface import Probe + + positions = np.array([[0, 0], [10, 10]]) + probe = Probe(ndim=2, si_units="um") + probe.set_contacts(positions=positions, shapes="circle", shape_params={"radius": 5}) + + with pytest.raises(ValueError, match="unique within a Probe"): + probe.set_contact_ids(["a", "a"]) + + +def test_set_contact_ids_rejects_wrong_size(): + """Setting contact_ids with wrong count raises ValueError.""" + from probeinterface import Probe + + positions = np.array([[0, 0], [10, 10]]) + probe = Probe(ndim=2, si_units="um") + probe.set_contacts(positions=positions, shapes="circle", shape_params={"radius": 5}) + + with pytest.raises(ValueError, match="do not have the same size"): + probe.set_contact_ids(["a", "b", "c"]) + + if __name__ == "__main__": test_probegroup() # ~ test_probegroup_3d()